UseState
- The useState hook is used to declare state variables in functional components. It allows us to read and update the state within the component.
Syntax
const [state, setState] = useState(initialState);
- state: The current value of the state.
- setState: A function used to update the state.
- initialState: The initial value of the state, which can be a primitive type or an object/array
useEffect:
The useEffect hook in React is used to handle side effects in functional components. It allows you to perform actions such as data fetching, DOM manipulation, and setting up subscriptions, which are typically handled in lifecycle methods like componentDidMount or componentDidUpdate in class components.
Syntax
useEffect(() => {
// Side effect logic here
}, [dependencies]);
useEffect(() => { ... }, [dependencies]);
runs side effects after rendering.
The effect runs based on changes in the specified dependencies.
UseContext
The useContext hook in React is a powerful and convenient way to consume values from the React Context API in functional components. It allows functional components to access context values directly, without the need to manually pass props down through the component tree
const contextValue = useContext(MyContext);
The useContext hook takes a context object (MyContext)
as an argument and returns the current value of that context.
The contextValue will hold the value provided by the nearest <MyContext.Provider>
in the component tree.
UseRef
The useRef Hook allows you to persist values between renders.
It can be used to store a mutable value that does not cause a re-render when updated.
It can be used to access a DOM element directly.
useRef() only returns one item. It returns an Object called current.
When we initialize useRef we set the initial value: useRef(0)
Top comments (0)