Making Components
You can create custom components to organize and maintain your code as well as share those components easily with the rest of the team.
Start by making a new file in src/components/index called MyName.jsx (generally component files should be capitalized). Then begin typing comp-func. If you have your snippets set up (see Getting Set Up) you should see a snippet appear.
Hit enter to see the code appear. Don't click anywhere. Just begin typing MyName (generally the name of the component should match the name of the file). This should appear in two places in the file, which should now look like this:
1
import React from 'react';
2
​
3
const MyName = props => (
4
<div />
5
);
6
​
7
export default MyName;
Copied!
Then, replace the <div /> with the following:
1
<p>Hello! My name is {props.name}.</p>
Copied!
The code {props.name} will display the name that is passed to the component once it's used (for more on React props check the official docs).
Now that it's created, you can use it in your src/components/index/page.js file.
First it must be imported with:
1
import MyName from './MyName';
Copied!
Then, you can use it in the JSX for your Page component like this:
1
<MyName name="Andrew" />
Copied!
After all that, you should have two files that look like this:
1
/* src/components/index/MyName.jsx */
2
import React from 'react';
3
​
4
const MyName = props => (
5
<p>Hello! My name is {props.name}.</p>
6
);
7
​
8
export default MyName;
Copied!
1
/* src/components/index/page.js */
2
​
3
// ... other imports
4
​
5
import MyName from './MyName';
6
​
7
class Page extends React.Component {
8
render() {
9
return (
10
// ...other elements
11
​
12
<MyName name="Andrew" />
13
​
14
// ...other elements
15
);
16
}
17
}
18
​
19
export default Page;
20
​
21
export { Helmet } from 'react-helmet';
Copied!
If you save this, you should see a paragraph on your page that reads:
1
Hello! My name is Andrew.
Copied!

Making Component Modules

Often custom components have their own components inside them. They may also have separate style files used to style them. In either case, you should make not just a component but a component module.
POLITICO component modules are directories that look like this:
1
ComponentName
2
β”œβ”€β”€ index.js
3
β”œβ”€β”€ styles.scss
4
└── ...OtherChildComponents
Copied!
For example, you can make a component called Greeting which has some styles, and contains your original MyName component as a child.
Create a file in src/components/index/Greeting/ called index.js. Then type: comp-func-styles and hit Enter to use your snippet. Without clicking away, type the word Greeting which should appear in three different places in your file.
Place MyName.jsx inside src/components/index/Greeting and then import it and use it index.js as described in the section above.
To add styles, create a file called styles.scss inside src/components/index/Greeting. Type comp and hit Enter to use your snippet. Any styles you put in this file will be mapped to only your Greeting component.
Now, you can use it in your src/components/index/page.js file.
Import it:
1
import Greeting from './Greeting';
Copied!
Then use it in the JSX for your Page component like this:
1
<Greeting />
Copied!
After all this, your files should look like this:
1
/* src/components/index/Greeting/MyName.jsx */
2
import React from 'react';
3
​
4
const MyName = props => (
5
<p>Hello! My name is {props.name}.</p>
6
);
7
​
8
export default MyName;
Copied!
1
/* src/components/index/Greeting/index.js */
2
import React from 'react';
3
import classnames from 'classnames';
4
​
5
import MyName from './MyName'
6
​
7
import { styles } from './styles.scss';
8
​
9
class Greeting extends React.Component {
10
constructor (props) {
11
super(props);
12
}
13
​
14
​
15
render () {
16
return (
17
<div className={classnames('Greeting', styles)}>
18
<MyName name="Andrew" />
19
</div>
20
);
21
}
22
}
23
​
24
export default Greeting;
Copied!
1
/* src/components/index/Greeting/styles.scss */
2
.styles :global {
3
​
4
}
Copied!
1
/* src/components/index/page.js */
2
​
3
// ... other imports
4
​
5
import Greeting from './Greeting';
6
​
7
class Page extends React.Component {
8
render() {
9
return (
10
// ...other elements
11
​
12
<Greeting />
13
​
14
// ...other elements
15
);
16
}
17
}
18
​
19
export default Page;
20
​
21
export { Helmet } from 'react-helmet';
Copied!
For more on the POLITICO component module style, check out those docs.
Last modified 2yr ago
Copy link