Defining the useUserState Hook

Repeating a similar process to what we did for the useTheme Hook, we import the useContext Hook from React and the StateContext. However, instead of returning the result of the Context Hook, we now pull out the state object via destructuring and then return state.user.

Create a new src/hooks/useUserState.js file with the following contents:

import { useContext } from 'react'
import { StateContext } from '../contexts'

export default function useUserState () {
const { state } = useContext(StateContext)
return state.user
}

Similarly to the useTheme Hook, the useUserState Hook makes our code more concise, easier to change later, and improves readability.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.101.60