What is useState() in React?
What is useState() in React?
useState is a Hook in React that allows you to add state to functional components. In class components, state is typically managed within the component using this.state and this.setState, but Hooks provide a way to work with stateful logic in functional components without converting them to classes.
When you call useState, you declare a state variable that React will preserve across re-renders. The useState Hook takes the initial state as its argument, which can be any data type, such as a string, number, object, or array. The initial state is only used during the first render.
Here's the basic syntax of useState:
const [state, setState] = useState(initialState);
state is the current state value, and it will be initialized to initialState on the first render.setState is a function that allows you to update state. When you call setState with a new value, React will re-render the component with the updated state.Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.