A hook just returns state and optionally a function that can mutate that state.
- Define the hook, here my hook is reading a token from
sessionStorage
and if it exists then set this as the initial state.
1 | import { useState } from 'react'; |
- Import and destruct the response from the hook
1 | import useToken from './useToken'; |
- Set the token value where
response
came from afetch
1 | setToken(response); |
Note that this is consuming the whole response, we could just pass in response.token
For context the complete reponse could like the below where token
is a valid JWT
1 | { |