Senior
What hooks can we use to store local state in React?
sobes.tech AI
Answer from AI
In React, the most commonly used hook for managing local state is useState. It allows you to create state within a functional component and update it.
Additionally, for more complex cases, you can use useReducer, which is suitable for managing state with more complex logic.
Example of using useState:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Counter: {count}</p>
<button onClick={() => setCount(count + 1)}>Increase</button>
</div>
);
}
Thus, the main hooks for local state are useState and useReducer.