Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. In fact, if you're creating a class component, you have to extend the base component from React. This requires more code but will also give you some benefits which you will see later on. But there are some benefits you get by using functional components in React: And so to answer our question before, you should use functional components if you are writing a presentational component which doesn’t have its own state or needs to access a lifecycle hook. If the next state depends on the current state, we recommend using the updater function form, instead: By default, when your component’s state or props change, your component will re-render. React lifecycle methods can be used inside class components (for example, componentDidMount ). See State and Lifecycle for more information about the state. If you take a look at the transpiled code by Babel you will also see some major differences: Edit (29.03.2019): This changed with the React 16.8 Hooks update! This page contains a detailed API reference for the React component class definition. Normally you should try to avoid all uses of forceUpdate() and only read from this.props and this.state in render(). Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them. Usually, you don’t need to set it explicitly because it’s inferred from the name of the function or class that defines the component. It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position. The constructor for a React component is called before it is mounted. Create an Event Responsive Dropdown List in React. Components defined as classes currently provide more features which are described in detail on this page. Use shouldComponentUpdate() to let React know if a component’s output is not affected by the current change in state or props. If you write a function component and realize you need to add some state to it, previously you had to convert it to a class component. The most obvious difference is the syntax. Do not rely on it to “prevent” a rendering, as this can lead to bugs. React will still only update the DOM if the markup changes. Our solution will be focusing on two key methods of react, createRef and more recently, the react hook useRef. You already get nice suggestions in VS Code: And errors when you compile without passing all required properties: If you wa… React lifecycle methods can be used inside class components (for example, componentDidMount). getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates. It is very inefficient and will harm performance. They are simple, purely functional and super easy to reason about.The following shows an example of a functional component with some typed properties. It enables your component to capture some information from the DOM (e.g. You can now use the useState hook to use state in your functional components. Otherwise, this.props will be undefined in the constructor, which can lead to bugs. A class component becomes an error boundary if it defines either (or both) of the lifecycle methods static getDerivedStateFromError() or componentDidCatch(). For example,