React Design Patterns and Best Practices

repository·master·Indexed 41 days ago

https://github.com/packtpublishing/react-design-patterns-and-best-practices

Supporting code and project files for the book 'React Design Patterns and Best Practices' by Packt. This resource provides implementation examples of real-world design patterns to improve React application flexibility, performance, and maintainability, including guidance on ES6 modules, Bootstrap integration, and build configurations using react-scripts.

Tokens
16.9K
Snippets
74
Records
81
Agent score
49%

What's inside React Design Patterns and Best Practices

  1. Automatic CSS post-processing

    master

    The build process automatically minifies CSS and adds vendor prefixes using Autoprefixer. You can write standard CSS, and the tool will handle cross-browser compatibility for properties like Flexbox.

    Example Input:

    .App {
      display: flex;
      flex-direction: row;
      align-items: center;
    }

    Example Output (after post-processing):

    .App {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-orient: horizontal;
      -webkit-box-direction: normal;
          -ms-flex-direction: row;
              flex-direction: row;
      -webkit-box-align: center;
              align-items: center;
    }

    Note: There is currently no support for CSS preprocessors like Less or sharing variables across CSS files.

  2. Understand the project folder structure

    master

    The project follows a standard Create React App structure. For the project to build successfully, the following files must exist with these exact names:

    • index.html: The page template.
    • favicon.ico: The browser tab icon.
    • src/index.js: The JavaScript entry point.

    Key Rules:

    • Processing: Only files inside src are processed by Webpack. To ensure your JS and CSS files are bundled, they must be placed inside src.
    • Subdirectories: You can create subdirectories within src to organize your code.
    • Top-level directories: You can create directories at the root level (outside src), but they will not be included in the production build (useful for documentation).
    my-app/
      README.md
      index.html
      favicon.ico
      node_modules/
      package.json
      src/
        App.css
        App.js
        index.css
        index.js
        logo.svg
  3. CSS Post-Processing with Autoprefixer

    master

    The build system automatically minifies CSS and adds vendor prefixes using Autoprefixer. You can write standard CSS, and the build process will handle cross-browser compatibility (e.g., adding -webkit- or -ms- prefixes) automatically.

    /* Input */
    .App {
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    
    /* Output (automatically prefixed) */
    .App {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      /* ... other prefixes ... */
    }
  4. Project folder structure and requirements

    master

    The project follows a standard structure. For the application to build correctly, the following files must exist with these exact filenames:

    • index.html: The page template.
    • favicon.ico: The browser tab icon.
    • src/index.js: The JavaScript entry point.

    Important Rules:

    • Place all JS and CSS files inside the src directory. Webpack only processes files within src. If files are outside src, they will not be included in the build.
    • You can create subdirectories inside src for organization.
    • You can create top-level directories (outside src) for non-build assets like documentation, but they will not be included in the production build.
    my-app/
      README.md
      index.html
      favicon.ico
      node_modules/
      package.json
      src/
        App.css
        App.js
        index.css
        index.js
        logo.svg
  5. Automatic CSS post-processing with Autoprefixer

    master

    The build system automatically minifies CSS and adds vendor prefixes using Autoprefixer. You can write standard CSS, and the build process will handle cross-browser compatibility (e.g., adding -webkit- or -ms- prefixes) automatically.

    /* Input CSS */
    .App {
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    
    /* Output CSS (automatically prefixed) */
    .App {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      flex-direction: row;
      /* ... other prefixes ... */
    }
  6. Configure ESLint for editor integration

    master

    While lint errors appear in the terminal and browser console, you can view them directly in editors like VS Code, Atom, or Sublime Text by installing an ESLint plugin for your editor.

    For Atom linter-eslint users, ensure the 'Use global ESLint installation' option is checked and that your package.json contains the following configuration:

    {
      "eslintConfig": {
        "extends": "./node_modules/react-scripts/config/eslint.js"
      }
    }

    To support this, you must install the following packages globally:

    npm install -g eslint babel-eslint eslint-plugin-react eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-flowtype
    npm install -g eslint babel-eslint eslint-plugin-react eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-flowtype
  7. Add and use stylesheets

    master

    The project uses Webpack to handle CSS. To associate a stylesheet with a component, import the CSS file directly inside the JavaScript file.

    Example:

    Button.css:

    .Button {
      padding: 20px;
    }

    Button.js:

    import React, { Component } from 'react';
    import './Button.css';
    
    class Button extends Component {
      render() {
        return <div className="Button" />;
      }
    }
    
    export default Button;

    Benefits and Limitations:

    • Development: Styles reload on the fly as you edit them.
    • Production: All CSS files are concatenated into a single minified .css file.
    • Portability: This approach uses Webpack-specific semantics. If you need to migrate to a different build tool later, consider putting all CSS in src/index.css and importing it from src/index.js.
    .Button {
      padding: 20px;
    }
    import React, { Component } from 'react';
    import './Button.css';
    
    class Button extends Component {
      render() {
        return <div className="Button" />;
      }
    }
    
    export default Button;
  8. Import and export components using ES6 modules

    master

    The project uses Babel to support ES6 modules. It is recommended to use import and export instead of require() and module.exports.

    Best Practices:

    • Default Exports: Use export default when a module exports a single entity (like a component). This allows you to import it without curly braces.
    • Named Exports: Use named exports for utility modules that export multiple functions.

    Example: Creating and importing a component

    1. Define the component (Button.js):
    import React, { Component } from 'react';
    
    class Button extends Component {
      render() {
        return <button>Click me</button>;
      }
    }
    
    export default Button;
    1. Import the component in another file (DangerButton.js):
    import React, { Component } from 'react';
    import Button from './Button';
    
    class DangerButton extends Component {
      render() {
        return <Button color="red" />;
      }
    }
    
    export default DangerButton;
  9. Add and import stylesheets

    master

    The project uses Webpack to handle assets. To associate a CSS file with a JavaScript component, import the CSS file directly inside the JavaScript file.

    Example:

    1. Create Button.css:
    .Button { padding: 20px; }
    1. Import it in Button.js:
    import './Button.css';
    
    class Button extends Component {
      render() {
        return <div className="Button" />;
      }
    }

    Note: This approach makes your code less portable to build tools other than Webpack. If you want to avoid Webpack-specific semantics, you can place all CSS in src/index.css and import it in src/index.js.

    import React, { Component } from 'react';
    import './Button.css';
    
    class Button extends Component {
      render() {
        return <div className="Button" />;
      }
    }
  10. Import images and fonts in JavaScript

    master

    You can import static assets like images and fonts directly into your JavaScript modules. Webpack will process the import and return a string representing the final URL/path of the asset in the compiled bundle. This allows you to use the imported variable in src attributes or CSS url() functions.

    Note: This is a Webpack-specific feature and may not be portable to environments like Node.js or Browserify.

    import React from 'react';
    import logo from './logo.png'; // Webpack returns the final path string
    
    function Header() {
      return <img src={logo} alt="Logo" />;
    }
    
    export default Header;