React Q&A

React.PureComponent

The difference between React.PureComponent and React.Component is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.

Functional vs Class-Components in React

  • Syntax
    • A functional component is just a plain JavaScript function which accepts props as an argument and returns a React element.
    • A class component requires you to extend from React.Component and create a render function which returns a React element.
  • Lifecycle Hooks
    • You can now use the useEffect hook to use lifecycle events in your functional components.
  • So why should I use functional components at all?
    1. Functional component are much easier to read and test because they are plain JavaScript functions without state or lifecycle-hooks
    2. You end up with less code
    3. They help you to use best practices.
    4. The React team mentioned that there may be a performance boost for functional component in future React versions.

What is the difference between state and props?

props (short for “properties”) and state are both plain JavaScript objects. While both hold information that influences the output of render, they are different in one important way: props get passed to the component (similar to function parameters) whereas state is managed within the component (similar to variables declared within a function).