Recoil js is another state management library for React.
Though we already have Redux. Mobx, Context, but here we got a new light entry in the community.
Why I am saying light you will figure it out at the end of this blog.
Why Recoil
- Firstly, it solves the global state management problems.
- Easy to learn; there are no new major principles n logic to learn.
- Quite Simple, it is similar like react.
- Though I like redux, but it will not take that much time to learn.
Recoil concepts
There are two major concepts:
- Atoms
- Selectors
Something new? Don’t worry will understand this quickly.
Atoms
This is similar to how we use the useState hook in react. If you are new to react hooks, checkout react hooks guide.
e.g.:
const [age, setAge] = useState(0);
age : this will be used for state variable declaration. setAge :this will be used for state variable updations.
1. Let’s start with binding root app with Recoil
Now you need to make a few alterations in your app’s root level, like the below snippet.
RecoilRoot will behave like a global context provider that will share the global state to your app tree.
Now update your older code snippet.
ReactDOM.render( <AppPage />, document.getElementById(“root”))
to
import { RecoilRoot } from “recoil”;
ReactDOM.render(
<RecoilRoot>
<AppPage />
</RecoilRoot>, document.getElementById(“root”))
2. Set the Atom
Recoil calls this part as atom, where we set the global key, value.
import { atom } from “recoil”;
const age = atom({
key: “age”,
default: 0 // default global value for age key
});
Now this atom will be accessible throughout the app.
3. use the atom
Now update your older code snippet.
const [age, setAge] = useState(0);
to
import { useRecoilState } from ‘recoil’
const [ageState, setAge] = useRecoilState(age);
For a Larger view, here is Full Code
import { atom,useRecoilState } from ‘recoil’
const age = atom({
key: “age”,
default: 0 // default global value for age key
});
export const AgeCalculator = () => {
const [ageState, setAge] = useRecoilState(age);
}
Quite easy. Isn’t it? Now let’s move to another part selectors
Read this blog post to learn in detail about selectors and how recoil.js is managing react.