In this lesson, I refactor a simple Counter component connected to Redux to use Unstated instead. I explain some of the cognitive overhead of working with Redux and how Unstated can help simplify your application codebase.

Additional Resources https://github.com/jamiebuilds/unstated

A basic example for Unstated:

/**
* Unstated Example
*/
import React from "react";
import ReactDOM from "react-dom";
import Counter from "./components/Counter";
import { Provider, Subscribe, Container } from "unstated"; class CounterContainer extends Container {
state = {
count: 0
}; increment() {
this.setState({ count: this.state.count + 1 });
} decrement() {
this.setState({ count: this.state.count - 1 });
}
} const ConnectedCounter = () => (
<Subscribe to={[CounterContainer]}>
{counter => (
<Counter
value={counter.state.count}
onIncrement={() => counter.increment()}
onDecrement={() => counter.decrement()}
/>
)}
</Subscribe>
); ReactDOM.render(
<Provider>
<ConnectedCounter />
</Provider>,
document.getElementById("root")
);

We use:

<Subscribe to={[CounterContainer]>

I means we want to keep the state for the component itself.

We can do some interesting things with <Provider> as well like dependency injection:

let counter = new CounterContainer();

render(
<Provider inject={[counter]}>
<Counter />
</Provider>
);

最新文章

  1. Mac OSX网络诊断命令
  2. 【转】nginx中的ngx_cdecl
  3. SQL Server 2008 R2——VC++ ADO 操作 重复利用_ParameterPtr
  4. 查看mysql版本的四种方法
  5. junit中的assert方法总结
  6. Centos7.5 java环境的安装配置
  7. 【已解决】Android ADT中增大AVD内存后无法启动:emulator failed to allocate memory 8
  8. 使用Windbg和SoS扩展调试分析.NET程序
  9. 无递归 A星寻路算法
  10. C++拾遗(二)关于变量
  11. gcc #define 学习记录
  12. android Fragment 用法小结
  13. iis虚拟目录或应用程序不继承父站点的web.config配置信息
  14. Javascript之学习笔记每日更新
  15. xCode8以及iOS10 的新特性
  16. R语言︱机器学习模型评价指标+(转)模型出错的四大原因及如何纠错
  17. nodejs常见问题
  18. 微信小程序调用地图选取位置后返回信息
  19. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
  20. Revit API创建一个拷贝房间内对象布局命令

热门文章

  1. kubernetes installing and using 单机版
  2. 前端-Node.js思维导图笔记
  3. mysql下载和安装Windows服务
  4. Nginx 监控
  5. C# invoke和begininvoke的用法
  6. 太坑了,mybatis注解一对多,id没了
  7. Split()函数
  8. 解决fixed在苹果手机抖动问题/头部底部固定布局
  9. Python 连接数据库 day5
  10. 《程序设计基础》实验题目2 c文件读取(反序列化?) 链表排序