Recoil is a state management library for React made by Facebook. It solves a specific problem: sharing state between components without prop drilling and without the boilerplate of Redux. If you have ever passed props through four components just to reach one button, Recoil is worth learning.
The two core ideas are atoms and selectors. An atom is a single piece of state:
import { atom , useRecoilState } from "recoil" ;
const countState = atom ( { key : "countState" , default : 0 , } ) ;
function Counter ( ) { const [ count , setCount ] = useRecoilState ( countState ) ; return < button onClick = { ( ) => setCount ( count + 1 ) } > { count } </ button > ; }
Enter fullscreen mode
Exit fullscreen mode
Any component can read or update that atom, no matter where it sits in the tree.β¦