How to use states in React.js?

How to use states in React.js »

In the previous section, you’ve successfully converted the functional component into a class based one. However, just doing that won’t solve the issue you had with re-rendering the newly updated content on the web browser. In this section, you’ll be guided with how to use states in React.js to update the states and show updated content on the browser. Before diving into that, there are some requirements of states that you must understand:

  1. React states can only be used in class based components and not in function based components
  2. State is nothing but a Javascript object that contains data for a component
  3. States and Props are completely different things and must not be confused with each other
  4. Updating a state will cause the component to immediately re-render
  5. States should be initialized with some default values when a component is created for the first time
  6. You can update states with setState() function only and cannot assign values to state variables directly

Leave a Comment