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 发布文章使用:只允许注册用户才可以访问!


Recoil & React official state management的更多相关文章

  1. angular2 学习笔记 ( 状态管理 state management )

    更新 : 2017-12-29  ng5 移除 zone.js https://zhuanlan.zhihu.com/p/29577461 zone 的用途就是拦截游览器事件, 比如 click, a ...

  2. Web前端的状态管理(State Management)

    背景 我相信很多朋友跟我一样,初次听到什么Flux, Redux, Vuex,状态管理的时候是一脸懵逼的.因为在外面之前前端大部分开发的时候,根本没有那么多的概念.自从ReactJS火爆后,什么Flu ...

  3. State management(状态管理)

    State management https://en.wikipedia.org/wiki/State_management UI控件的状态管理, 例如按钮的灰化.只读.显示隐藏等. 特殊地一个控件 ...

  4. react 中state与props

    react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...

  5. .NET:CLR via C# Exceptions and State Management

    重点学习的个概念 unhandled exceptions constrained execution regions code contracts runtime wrapped exception ...

  6. [React] Update State in React with Ramda's Evolve

    In this lesson we'll take a stateful React component and look at how we can refactor our setState ca ...

  7. React给state赋值的两种写法

    如果你看过React的官方文档,就会对怎么给局部state赋值有一定的了解.如下代码: class Test extends React.Component { constructor(props) ...

  8. React:Lifting State Up

    在学习React的组件的时候,我也好奇组件间需要共享状态或通信的时候,React是如何处理的.在文档的QUICK START的提到Lifting State Up(状态提升),并不是什么新鲜东西.只是 ...

  9. React & update state with props & Object.assign

    React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...

随机推荐

  1. nginx.service: control process exited, code=exited status=1

    安装linux的宝塔面板,结果面板显示nginx和php已经运行了,但是机器系统上并没有运行.记录一次nginx报错,操作步骤看下代码: [root@localhost nginx]# systemc ...

  2. 如何使用 Vuepress

    项目结构 ├─docs │ ├─.vuepress --vuepress相关文件路径 (主要配置) │ │ ├─dist --build 打包生成路径 (自定义) │ │ ├─nav --导航条配置 ...

  3. HTML Standard系列:Event loop、requestIdleCallback 和 requestAnimationFrame

    HTML Standard系列:Event loop.requestIdleCallback 和 requestAnimationFrame - 掘金 https://juejin.im/post/5 ...

  4. 进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道

    管道 Linux环境进程间通信(一) https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/index.html 管道及有名管道 郑彦兴200 ...

  5. Feign配置日志的打印级别

    一.细粒度的配置Feign的日志级别(针对每个微服务配置) 1.java代码方式 (1)在Feign接口注解上面配置configuration /** * @author : maybesuch * ...

  6. 根据pom标签修改

    sed -i "s/<count>1<\/count>/<count>2<\/count>/g"  pom.xml

  7. 13.Linux文件存储系统

    1.Linux 系统中的文件存储结构 Linux系统中常见的目录名称以及相应内容 2.系统内核中的udev 设备管理器会自动把硬件名称规范起来,目的是让用户通过设备文件的名字可以猜出设备大致的属性以及 ...

  8. NoSQL:一个帝国的崛起

    01关系数据库帝国 现在是公元2009年,关系帝国已经统治了我们30多年,实在是太久了. 1970年,科德提出关系模型,1974年张伯伦和博伊斯制造出了SQL ,帝国迅速建立起了统治. 从北美到欧洲, ...

  9. Codeforces错题本

    为什么我这么菜啊QAQ Codeforces 1364C

  10. 牛客练习赛64 D【容斥+背包】

    牛客练习赛64 D.宝石装箱 Description \(n\)颗宝石装进\(n\)个箱子使得每个箱子中都有一颗宝石.第\(i\)颗宝石不能装入第\(a_i\)个箱子.求合法的装箱方案对\(99824 ...