Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should every react component be in an individual file?

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>
    );
  }
}
like image 827
Aspen Avatar asked Mar 23 '17 23:03

Aspen


People also ask

Should each React component be a separate file?

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.

How many React components are in a file?

Only include one React component per file. However, multiple Stateless, or Pure, Components are allowed per file. eslint: react/no-multi-comp .

Should each React component have its own CSS file?

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.

Does React have single file components?

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.


1 Answers

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>
like image 182
Brian Avatar answered Oct 20 '22 16:10

Brian