Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Created June 23, 2021 13:21
Show Gist options
  • Save VitorLuizC/fdb8c01280d3503217e4d6c9cc2c5a7c to your computer and use it in GitHub Desktop.
Save VitorLuizC/fdb8c01280d3503217e4d6c9cc2c5a7c to your computer and use it in GitHub Desktop.

Sobre a definição de React.js components

React.js components podem ser definidos de diferentes formas, entre elas:

  • Como classes filhas das classes Component e PureComponent.

    import { PureComponent } from 'react';
    
    class Button extends PureComponent {
      static defaultProps = {
        type: 'button',
      };
    
      render() {
        const { type, children, onClick } = this.props;
        return (
          <button type={type} onClick={onClick}>
            {children}
          </button>
        );
      }
    }
  • Como retorno das funções forwardRef e memo.

    import { forwardRef } from 'react';
    
    const Button = forwardRef(
      function Button(props, ref) {
        const { type, children, onClick } = props;
    
        return (
          <button ref={ref} type={type} onClick={onClick}>
            {children}
          </button>
        );
      },
    );
  • Como funções que recebem um objeto com as props como argumento e retornam JSX (ou null).

    function Button(props) {
      const { type, children, onClick } = props;
    
      return (
        <button type={type} onClick={onClick}>
          {children}
        </button>
      );
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment