React interview cheatsheet series
Hooks are a new addition in React 16.8. and allows us to write functional programming (with function component) instead of class oriented programming (class components).
The basic hooks are:
- useState(). Which replaces the state of the class component types.
- useEffects(). Which replaces the lifecycle methods like componentDidMount, componentDidUpdate, componentWillUnmount of the class component types.
- useContext(). Which is used in Context API
Another advanced hooks are:
The logic behind useState and useEffect is pretty clear and better adding just a code block that will explain how they work:
About useContext(), accepts a context object (the value returned from React.createContext) and returns the current context value for that context. The current context value is determined by the value prop of the nearest <MyContext.Provider> above the calling component in the tree.
This is the offical example at documentation: