Recoil & React official state management

Redux

Recoil.js

https://recoiljs.org/

A state management library for React

$ npx create-react-app my-app

$ npm init react-app my-app

$ yarn create react-app my-app
$ yarn add recoil

https://github.com/facebook/create-react-app#creating-an-app

RecoilRoot

import React from 'react';
import {
RecoilRoot,
atom,
selector,
useRecoilState,
useRecoilValue,
} from 'recoil'; function App() {
return (
<RecoilRoot>
<CharacterCounter />
</RecoilRoot>
);
}

Atom

const textState = atom({
key: 'textState', // unique ID (with respect to other atoms/selectors)
default: '', // default value (aka initial value)
});
function CharacterCounter() {
return (
<div>
<TextInput />
<CharacterCount />
</div>
);
} function TextInput() {
const [text, setText] = useRecoilState(textState); const onChange = (event) => {
setText(event.target.value);
}; return (
<div>
<input type="text" value={text} onChange={onChange} />
<br />
Echo: {text}
</div>
);
}

Selector

const charCountState = selector({
key: 'charCountState', // unique ID (with respect to other atoms/selectors)
get: ({get}) => {
const text = get(textState); return text.length;
},
});
function CharacterCount() {
const count = useRecoilValue(charCountState); return <>Character Count: {count}</>;
}

refs

https://mp.weixin.qq.com/s/EWcTOB08oU96OcQ-41FbPA

Recoil === 畏缩 / 后坐力



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. python排序之一插入排序
  2. 把Tomcat注册为windows服务
  3. 利用缓存实现APP端与服务器接口交互的Session控制
  4. Flexbox,更优雅的布局
  5. node.js express的安装过程
  6. vs2008破解方法
  7. 关于在asp.net的web页面中的全局变量问题
  8. 移动网络山寨版(OpenBTS)的意义或者无意义 【1】
  9. 深入分析MySQL ERROR 1045 (28000)
  10. 最短路径算法-Dijkstra算法的应用之单词转换(词梯问题)(转)
  11. Online Judge(OJ)搭建——1、项目介绍
  12. Python读取配置文件,并连接数据库SQL Server
  13. django 表单过滤与查询
  14. 数据库历险记(一) | MySQL这么好,为什么还有人用Oracle?
  15. 在递归函数中使用JQuery.Deferred,异步请求中的同步执行...
  16. 阿里八八Beta冲刺博客集合贴
  17. Retrofit2 项目配置
  18. 【原创】QT简单计算器
  19. 轻量级web富文本框——wangEditor使用手册(1)——基本应用 demo
  20. HDU2643(SummerTrainingDay05-P 第二类斯特林数)

热门文章

  1. 前端面试之JavaScript中this的指向【待完善!】
  2. notepad文件对比
  3. 解读腾讯敏捷研发核心驱动力 腾讯TAPD TAPD 2020-12-17
  4. OPTIONS的预请求(Preflighted Request)
  5. P5858 Golden Swold
  6. 最简单直接地理解Java软件设计原则之开闭原则
  7. 一:优化Docker中的Spring Boot应用:单层镜像方法
  8. Java Web学习之路
  9. Spring MVC—模型数据,转发重定向,静态资源处理方式
  10. 复制虚拟机,链接网络问题:没有找到合适的设备:没有找到可用于链接System eth0 的