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