Read a Value from Recoil with useRecoilValue

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet

To get a value from Recoil we can import the atom that we want and the hook useRecoilValue from recoil.

Then we can get the value:

import { useRecoilValue } from 'recoil'
...
import { gameScore } from './atoms'
...
const score = useRecoilValue(gameScore)

And display it in a lablel:

<h3>Score: {score}</h3>

That value will update every time the button is clicked (and setScore is called) automatically!