Recoil

Get Started

npm install recoil

Introduction

  • Recoil is a state management library developed by facebook

  • Similar with context, but it can pass multiple state, but also re-render for children who subscribe for the state

Atom

  • It is a state which is used to share to children

import {
  RecoilRoot,
  atom,
  selector
} from 'recoil';

const textState = atom({
  key: 'textState', // unique ID (with respect to other atoms/selectors)
  default: 'test', // default value (aka initial value)
});

function App() {
  return (
    <RecoilRoot>
       <TextInput  textState={textState}/>
  </RecoilRoot>
  );
  • We can change the value just like useState

Selector

  • It is a pure function that take the atom as a input

  • The return value of selector cannot be change directly, so it is recommended to use useRecoilValue to get the return value only

Data Fetching

  • Data Fetching is mostly done on selector

  • Data Fetching can be used with react suspense to show loading

  • But also used show loading by using hook

  • Data Fetching can also take variable as a parameter on api calling instead of atom

Reference

Last updated

Was this helpful?