They appear to be in the same file in the documentation example: https://facebook.github.io/react/docs/thinking-in-react.html.
How would you break up your components into pages in an instance like this where some of your components are very small?
class ProductCategoryRow extends React.Component {
render() {
return (<tr><th colSpan="2">{this.props.category}</th></tr>);
}
}
class ProductRow extends React.Component {
render() {
var name = this.props.product.stocked ?
this.props.product.name :
<span style={{color: 'red'}}>
{this.props.product.name}
</span>;
return (
<tr>
<td>{name}</td>
<td>{this.props.product.price}</td>
</tr>
);
}
}
class ProductTable extends React.Component {
render() {
var rows = [];
var lastCategory = null;
this.props.products.forEach((product) => {
if (product.name.indexOf(this.props.filterText) === -1 || (!product.stocked && this.props.inStockOnly)) {
return;
}
if (product.category !== lastCategory) {
rows.push(<ProductCategoryRow category={product.category} key={product.category} />);
}
rows.push(<ProductRow product={product} key={product.name} />);
lastCategory = product.category;
});
return (
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
}
All components should be inside components directory Keep each component in a separate file. If you need to have styles etc. for the component then create a folder for the component.
Only include one React component per file. However, multiple Stateless, or Pure, Components are allowed per file. eslint: react/no-multi-comp .
CSS Modules # A CSS Module is a CSS file in which all class names and animation names are scoped locally by default. When using CSS Modules, each React component is provided with its own CSS file, that is scoped to that file and component alone.
May 2021 edit: the Remix framework from the React Router duo also adopt a single file component format that involves exporting links , loader , and action . The launch of RedwoodJS today marks a first: it is the first time React components are being expressed in a single file format with explicit conventions.
You may not have to put each component in its own file - for example, you could further split the ProductRow component using stateless functional components:
const ProductName = (product) =>
<span style={{color: product.stocked ? 'black' : 'red'}}>
{ product.name }
</span>
const ProductRow = (product) =>
<tr>
<td>{ ProductName(product) }</td>
<td>{ product.price }</td>
</tr>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With