State a component memory Flashcards
What is a state variable used for in a component?
To remember some information between renders.
State variables store data that can change over time within a component.
How are state variables declared in React?
By calling the useState Hook.
The useState Hook is a built-in function that allows you to add state to functional components.
What are Hooks in React?
Special functions that start with use, allowing you to hook into React features like state.
Common hooks include useState, useEffect, and useContext.
Where must Hooks be called in a React component?
At the top level of a component or another Hook.
Hooks should not be called conditionally or inside loops.
What does the useState Hook return?
A pair of values: the current state and the function to update it.
This allows for both reading and updating the state in the component.
Can you have more than one state variable in a React component?
Yes, you can have more than one state variable.
React matches them up by their order of declaration.
Is state private to a component in React?
Yes, state is private to the component.
Each instance of a component maintains its own state.