🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Expert Masterclasses.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

React State

Master React components, hooks, and best practices.

State

Author

Pascual Vila

Frontend Instructor.

State is a set of data that belongs to a component and can change over time. When the state changes, React re-renders the component with the new data. State is mutable and is defined within the component using the useState hook.

Using State with useState:

  • The useState hook returns two values: the current state value and a function to update that value.
  • State can hold any type of data, such as numbers, strings, objects, or arrays.

Example of State usage:

              {`import React, { useState } from "react";
      
              function Counter() {
                const [count, setCount] = useState(0);
      
                return (
                  <div>
                    <p>You clicked {count} times.</p>
                    <button onClick={() => setCount(count + 1)}>Click me</button>
                  </div>
                );
              }
      
              export default Counter;`}