Hooks
Hooks
There are 10 in-built hooks that was shipped with React 16.8 but the basic
(commonly used) hooks include:
useState()
useEffect()
useContext()
useReducer()
These are the 4 basic hooks that are commonly used by React developers that have
adopted React Hooks into their codebases.
useState()
The useState() hook allows React developers to update, handle and manipulate
state inside functional cpomponents without needing to convert it to a class
component. Let’s use the code snippet below is a simple Age counter component
and we will use it to explain the power and syntax of the useState() hook.
useState:
useEffect()
The useEffect() hook accepts a function that would contain effectual code.
useEffect() hook as component mounting, updating and unmounting — all
combined in one function. It lets us replicate the lifecycle methods in functional
components.
useContext()
The useContext() hook accepts a context object, i.e the value that is returned
from React.createContext, and then it returns the current context value for that
context.
function Display() {
const value = useContext(NumberContext);
return <div>The answer is {value}.</div>;
}