SlideShare a Scribd company logo
Manage React State with
Recoil
Yoni Weisbrod
Lemonade Frontend Guild, Sept. 2020
Recoil in brief
- State manager by Facebook
- Some of the API is still unstable 🚧
- Strong integration with React
(e.g. hooks & Suspense)
Docs at https://recoiljs.org/
Getting Acquainted
● Install RecoilJS
Install RecoilJS
- npm install recoil
- Surround node with
<RecoilRoot>
Fin.
Getting Acquainted
● Install RecoilJS
● Refactor from useState to useRecoilState
The atom
key
default
useState
useRecoilState
State stored
outside the
component
Selectors
● Can access
state from
atoms or
selectors
● Always
freshtm
● Derived state
Recoil Dependency Graph
- State is consumed using built-in hooks (e.g.
useRecoilState, useRecoilValue, useSetRecoilState,
etc.)
- Each hook has an appropriate use case.
Recoil in Action: Consuming State
Recoil in Action: Consuming State
- State is consumed using built-in hooks (e.g.
useRecoilState, useRecoilValue, useSetRecoilState,
etc.)
- Each hook has an appropriate use case.
- atoms and selectors cannot be consumed directly.
- State is consumed using built-in hooks
⚠️ Limitation: Only React components can consume Recoil state.
Recoil in Action: Consuming State
Hook #1: useRecoilState
● Most similar to useState (provides getter and setter objects)
● Triggers a re-render when subscribed atom/selector changes
Example
useRecoilState vs.
useSetRecoilState vs.
useRecoilValue
Demo: https://codesandbox.io/s/the-right-hook-lemonade-mtmpo
Asynchronous Selectors
Asynchronous Selectors
Q: What will be rendered until the DB query returns a value?
Asynchronous Selectors
Q: What will be rendered until the DB query returns a value?
A: Error
Asynchronous Selectors
Solution 1 - React.Suspense
Asynchronous Selectors
Solution 1 - React.Suspense
Solution 1 - Avoid returning a promise
useRecoilValueLoadable
Reactions
- useEffect
Reactions
- Asynchronous selectors
- useEffect
Reactions
- Asynchronous selectors: cached, but cross-application
- useEffect: non-cached, but local
Thanks!
Questions?

More Related Content

What's hot

Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
Bethmi Gunasekara
 
React js
React jsReact js
React js
Alireza Akbari
 
Redux toolkit
Redux toolkitRedux toolkit
Redux toolkit
ofoefiergbor1
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
Imran Sayed
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
Jimit Shah
 
What is component in reactjs
What is component in reactjsWhat is component in reactjs
What is component in reactjs
manojbkalla
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
Cuong Ho
 
Redux essentials
Redux essentialsRedux essentials
Redux essentials
Chandan Kumar Rana
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
kiranabburi
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
Thanh Tuong
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
Knoldus Inc.
 
react redux.pdf
react redux.pdfreact redux.pdf
react redux.pdf
Knoldus Inc.
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
Vedran Blaženka
 
reactJS
reactJSreactJS
reactJS
Syam Santhosh
 
Intro to React
Intro to ReactIntro to React
Intro to React
Justin Reock
 
Ngrx
NgrxNgrx
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
Samundra khatri
 
React js
React jsReact js
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
Soluto
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 

What's hot (20)

Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
React js
React jsReact js
React js
 
Redux toolkit
Redux toolkitRedux toolkit
Redux toolkit
 
Redux workshop
Redux workshopRedux workshop
Redux workshop
 
React.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOMReact.js - The Dawn of Virtual DOM
React.js - The Dawn of Virtual DOM
 
What is component in reactjs
What is component in reactjsWhat is component in reactjs
What is component in reactjs
 
Introduction to react and redux
Introduction to react and reduxIntroduction to react and redux
Introduction to react and redux
 
Redux essentials
Redux essentialsRedux essentials
Redux essentials
 
Introduction to react
Introduction to reactIntroduction to react
Introduction to react
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
react redux.pdf
react redux.pdfreact redux.pdf
react redux.pdf
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
reactJS
reactJSreactJS
reactJS
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Ngrx
NgrxNgrx
Ngrx
 
Understanding react hooks
Understanding react hooksUnderstanding react hooks
Understanding react hooks
 
React js
React jsReact js
React js
 
React new features and intro to Hooks
React new features and intro to HooksReact new features and intro to Hooks
React new features and intro to Hooks
 
How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...How to implement internationalization (i18n) in angular application(multiple ...
How to implement internationalization (i18n) in angular application(multiple ...
 

Similar to Manage React State with Recoil

Understanding State In Compose.pptx
Understanding State In Compose.pptxUnderstanding State In Compose.pptx
Understanding State In Compose.pptx
Ankitkumar822995
 
Java EE web project introduction
Java EE web project introductionJava EE web project introduction
Java EE web project introduction
Ondrej Mihályi
 
React workshop
React workshopReact workshop
React workshop
Imran Sayed
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
Flipkart
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
tuanpa206
 
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React NativeGITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Indonesia
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit Testing
Eswara Kumar Palakollu
 
Reactive state management with Jetpack Components
Reactive state management with Jetpack ComponentsReactive state management with Jetpack Components
Reactive state management with Jetpack Components
Gabor Varadi
 
Pluginkla2019 - React Presentation
Pluginkla2019 - React PresentationPluginkla2019 - React Presentation
Pluginkla2019 - React Presentation
Angela Lehru
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
dhanushkacnd
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
StephieJohn
 
React Workshop: Core concepts of react
React Workshop: Core concepts of reactReact Workshop: Core concepts of react
React Workshop: Core concepts of react
Imran Sayed
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
NavneetKumar111924
 
React state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of HooksReact state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of Hooks
IRJET Journal
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples
Ravi Mone
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
StephieJohn
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
Jainul Musani
 
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOWAdvanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Mateusz Bosek
 
Getting Started With ReactJS
Getting Started With ReactJSGetting Started With ReactJS
Getting Started With ReactJS
Sandeep Kumar Patel
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 

Similar to Manage React State with Recoil (20)

Understanding State In Compose.pptx
Understanding State In Compose.pptxUnderstanding State In Compose.pptx
Understanding State In Compose.pptx
 
Java EE web project introduction
Java EE web project introductionJava EE web project introduction
Java EE web project introduction
 
React workshop
React workshopReact workshop
React workshop
 
Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"Presentation on "An Introduction to ReactJS"
Presentation on "An Introduction to ReactJS"
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
 
GITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React NativeGITS Class #20: Building A Fast and Responsive UI in React Native
GITS Class #20: Building A Fast and Responsive UI in React Native
 
React and Flux life cycle with JSX, React Router and Jest Unit Testing
React and  Flux life cycle with JSX, React Router and Jest Unit TestingReact and  Flux life cycle with JSX, React Router and Jest Unit Testing
React and Flux life cycle with JSX, React Router and Jest Unit Testing
 
Reactive state management with Jetpack Components
Reactive state management with Jetpack ComponentsReactive state management with Jetpack Components
Reactive state management with Jetpack Components
 
Pluginkla2019 - React Presentation
Pluginkla2019 - React PresentationPluginkla2019 - React Presentation
Pluginkla2019 - React Presentation
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
React Workshop: Core concepts of react
React Workshop: Core concepts of reactReact Workshop: Core concepts of react
React Workshop: Core concepts of react
 
downloads_introduction to redux.pptx
downloads_introduction to redux.pptxdownloads_introduction to redux.pptx
downloads_introduction to redux.pptx
 
React state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of HooksReact state management and side-effects – A Review of Hooks
React state management and side-effects – A Review of Hooks
 
ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples ReactJS Component Lifecycle hooks with examples
ReactJS Component Lifecycle hooks with examples
 
Fundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdfFundamental Concepts of React JS for Beginners.pdf
Fundamental Concepts of React JS for Beginners.pdf
 
React js t6 -lifecycle
React js   t6 -lifecycleReact js   t6 -lifecycle
React js t6 -lifecycle
 
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOWAdvanced Redux architecture - WHAT/WHEN/WHY/HOW
Advanced Redux architecture - WHAT/WHEN/WHY/HOW
 
Getting Started With ReactJS
Getting Started With ReactJSGetting Started With ReactJS
Getting Started With ReactJS
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 

Recently uploaded

cyber crime.pptx..........................
cyber crime.pptx..........................cyber crime.pptx..........................
cyber crime.pptx..........................
GNAMBIKARAO
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
3a0sd7z3
 
KubeCon & CloudNative Con 2024 Artificial Intelligent
KubeCon & CloudNative Con 2024 Artificial IntelligentKubeCon & CloudNative Con 2024 Artificial Intelligent
KubeCon & CloudNative Con 2024 Artificial Intelligent
Emre Gündoğdu
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
rtunex8r
 
Bangalore Call Girls 9079923931 With -Cuties' Hot Call Girls
Bangalore Call Girls 9079923931 With -Cuties' Hot Call GirlsBangalore Call Girls 9079923931 With -Cuties' Hot Call Girls
Bangalore Call Girls 9079923931 With -Cuties' Hot Call Girls
narwatsonia7
 
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
APNIC
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
Tarandeep Singh
 
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
APNIC
 
Decentralized Justice in Gaming and Esports
Decentralized Justice in Gaming and EsportsDecentralized Justice in Gaming and Esports
Decentralized Justice in Gaming and Esports
Federico Ast
 
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
dtagbe
 
How to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdfHow to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdf
Infosec train
 
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
thezot
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
3a0sd7z3
 

Recently uploaded (13)

cyber crime.pptx..........................
cyber crime.pptx..........................cyber crime.pptx..........................
cyber crime.pptx..........................
 
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
快速办理(Vic毕业证书)惠灵顿维多利亚大学毕业证完成信一模一样
 
KubeCon & CloudNative Con 2024 Artificial Intelligent
KubeCon & CloudNative Con 2024 Artificial IntelligentKubeCon & CloudNative Con 2024 Artificial Intelligent
KubeCon & CloudNative Con 2024 Artificial Intelligent
 
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
怎么办理(umiami毕业证书)美国迈阿密大学毕业证文凭证书实拍图原版一模一样
 
Bangalore Call Girls 9079923931 With -Cuties' Hot Call Girls
Bangalore Call Girls 9079923931 With -Cuties' Hot Call GirlsBangalore Call Girls 9079923931 With -Cuties' Hot Call Girls
Bangalore Call Girls 9079923931 With -Cuties' Hot Call Girls
 
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
Honeypots Unveiled: Proactive Defense Tactics for Cyber Security, Phoenix Sum...
 
Bengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal BrandingBengaluru Dreamin' 24 - Personal Branding
Bengaluru Dreamin' 24 - Personal Branding
 
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...Securing BGP: Operational Strategies and Best Practices for Network Defenders...
Securing BGP: Operational Strategies and Best Practices for Network Defenders...
 
Decentralized Justice in Gaming and Esports
Decentralized Justice in Gaming and EsportsDecentralized Justice in Gaming and Esports
Decentralized Justice in Gaming and Esports
 
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
一比一原版(uc毕业证书)加拿大卡尔加里大学毕业证如何办理
 
How to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdfHow to make a complaint to the police for Social Media Fraud.pdf
How to make a complaint to the police for Social Media Fraud.pdf
 
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
一比一原版新西兰林肯大学毕业证(Lincoln毕业证书)学历如何办理
 
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
快速办理(新加坡SMU毕业证书)新加坡管理大学毕业证文凭证书一模一样
 

Manage React State with Recoil

Editor's Notes

  1. Recoil is a state manager built internally at Facebook. It’s still experimental - meaning some of the API is in flux. Stability - can I use it in production? Much of the API is fine, stable. But some parts of it are still unstable. I would not build a business around it just yet personally, as it’s still nowhere near version 1. Currently at v0.0.12. Dependent on React - can’t be used outside
  2. Don’t have to wrap entire app with RecoilRoot - can use it on a specific node. Wrap outer node
  3. Regular React useState
  4. Regular React useState
  5. Only 2 changes - We’ve added a state object (an atom) We’ve changed useState to useRecoilState LIFTS state out of view
  6. Selectors Similar to Redux selectors -> way to get derived data Keeps data always fresh
  7. Analogy - spreadsheet.
  8. React components - including custom hooks. Meaning you can’t consume state inside a regular JS function, like you can with MobX. This is a limitation of Recoil - it lifts state out of components, but not outside React.
  9. Only 2 changes - We’ve added a state object (an atom) We’ve changed useState to useRecoilState
  10. Egghead lesson - https://egghead.io/lessons/react-choose-the-right-hook-for-your-recoil-state Fork sandbox, start with useState, then go to the rest.
  11. Async selectors - can be used to make API calls, DB queries, etc. Why? Keeps state up to date. Could also just use useEffect to make API calls based on Reactions.
  12. Async selectors - can be used to make API calls, DB queries, etc. Why? Keeps state up to date. Could also just use useEffect to make API calls based on Reactions.
  13. Only 2 changes - We’ve added a state object (an atom) We’ve changed useState to useRecoilState
  14. Only 2 changes - We’ve added a state object (an atom) We’ve changed useState to useRecoilState
  15. Only 2 changes - We’ve added a state object (an atom) We’ve changed useState to useRecoilState
  16. Only 2 changes - We’ve added a state object (an atom) We’ve changed useState to useRecoilState
  17. Cons: Local (not cross-application, so you can’t leverage the lifting out of state of Recoil) Pros: Not cached… (explain in next slide)
  18. Cons: CACHED!! Good for avatars, etc. Pros: Can be shared across app