How To Use (More) React Hooks

useContext

import React, { createContext } from 'reactconst SampleContext = createContext()
<SampleContext.Provider value={'sample'}>
{children}
</SampleContext.Provider>
const sample = useContext(SampleContext)
const UserContext = createContext()const UserContextProvider = ({ children }) => {
const [user, setUser] = useState(null)
const userId = localStorage.getItem('userId')

useEffect(() => {
if(userId) {
getUser(userId)
.then(resp => resp.json())
.then(userData => storeUser(userData))
}, []}
const storeUser = userData => {
setUser(userData)
localStorage.setItem('userId', userData.id)
}
return (
<UserContext.Provider value={{ user, storeUser }})>
{children}
</UserContext.Provider>
}
const { user, storeUser } = useContext(UserContext)

useRef

const buttonRef = useRef(null)
<button ref={buttonRef}>Click Me</button>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store