How you will maintain the state in React, explain me with examples ?
You can use Redux to maintain state in React.
An example of how to use useState:
import React, { useState } from 'react';const Counter = () => { const [count, setCount] = useState(0); const handleClick = () => { setCount(count + 1); }; return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment</button> </div> );};export default Counter;
import React, { useState } from 'react';
const Counter = () => {
const [count, setCount] = useState(0);
const handleClick = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={handleClick}>Increment</button>
</div>
);
export default Counter;
We have a functional component called Counter that maintains a count state using useState. We initialize the count state to 0 using useState(0). We also define a function called handleClick that updates the count state by calling setCount with the new value. In the return statement, we render the current value of count using JSX. We also render a button that calls the handleClick function when clicked.
In React, you can maintain state using the useState hook. The useState hook is a built-in function that lets you add state to functional components in React. It takes an initial value as an argument and returns an array containing the current state value and a function to update the state.
maintaining state in react js we use a redux use for state management, redux is a open source library