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:

  1. /**
  2. * Unstated Example
  3. */
  4. import React from "react";
  5. import ReactDOM from "react-dom";
  6. import Counter from "./components/Counter";
  7. import { Provider, Subscribe, Container } from "unstated";
  8.  
  9. class CounterContainer extends Container {
  10. state = {
  11. count: 0
  12. };
  13.  
  14. increment() {
  15. this.setState({ count: this.state.count + 1 });
  16. }
  17.  
  18. decrement() {
  19. this.setState({ count: this.state.count - 1 });
  20. }
  21. }
  22.  
  23. const ConnectedCounter = () => (
  24. <Subscribe to={[CounterContainer]}>
  25. {counter => (
  26. <Counter
  27. value={counter.state.count}
  28. onIncrement={() => counter.increment()}
  29. onDecrement={() => counter.decrement()}
  30. />
  31. )}
  32. </Subscribe>
  33. );
  34.  
  35. ReactDOM.render(
  36. <Provider>
  37. <ConnectedCounter />
  38. </Provider>,
  39. document.getElementById("root")
  40. );

We use:

  1. <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:

  1. let counter = new CounterContainer();
  2.  
  3. render(
  4. <Provider inject={[counter]}>
  5. <Counter />
  6. </Provider>
  7. );

[React] Refactor a connected Redux component to use Unstated的更多相关文章

  1. [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug

    In this lesson we'll look at React PowerPlug's <List /> component by refactoring a normal clas ...

  2. 如何在非 React 项目中使用 Redux

    本文作者:胡子大哈 原文链接:https://scriptoj.com/topic/178/如何在非-react-项目中使用-redux 转载请注明出处,保留原文链接和作者信息. 目录 1.前言 2. ...

  3. 【前端】react学习阶段总结,学习react、react-router与redux的这些事儿

    前言 借用阮一峰的一句话:真正学会 React 是一个漫长的过程. 这句话在我接触react深入以后,更有感触了.整个react体系都是全新的,最初做简单的应用,仅仅使用react-tools打包js ...

  4. 如何优雅地在React项目中使用Redux

    前言 或许你当前的项目还没有到应用Redux的程度,但提前了解一下也没有坏处,本文不会安利大家使用Redux 概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与 ...

  5. 优雅的在React项目中使用Redux

    概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与React没有任何关系,其他UI框架也可以使用Redux react-redux React插件,作用:方便在 ...

  6. 前端笔记之React(五)Redux深入浅出

    一.Redux整体感知 Redux是JavaScript状态管理容器,提供了可被预测状态的状态管理容器.来自于Flux思想,Facebook基于Flux思想,在2015年推出Redux库. 中文网站: ...

  7. react,react-router,redux+react-redux 构建一个React Demo

    创建初始化应用 加速我们的npm. npm install -g cnpm --registry=https://registry.npm.taobao.org 利用create-react-app ...

  8. react、react-router、redux 也许是最佳小实践1

    小前言 这是一个小小的有关react的小例子,希望通过一个小例子,可以让新手更好的了解到react.react-router4.0.redux的集中使用方法. 这是基于create-react-app ...

  9. 【转】浅谈React、Flux 与 Redux

    本文转自<浅谈React.Flux 与 Redux>,转载请注明出处. React React 是一个 View 层的框架,用来渲染视图,它主要做几件事情: 组件化 利用 props 形成 ...

随机推荐

  1. scala 变量定义,基本操作符

    scala是jvm语言,它将面向对象与函数风格结合的很好,它可以访问任何java类库并很好的结合使用. 它可以使程序更简单,同时可利用并发的威力. scala基本语法: package com.tes ...

  2. React容器组件和展示组件

    Presentational and Container Components   展示组件   - 只关心它们的样子.   - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式.   ...

  3. Angular——表单指令

    基本介绍 这些指定只能针对input标签 基本使用 <!DOCTYPE html> <html lang="en"> <head> <me ...

  4. 六时出行 App 隐私政策

    六时出行 App 隐私政策   本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义 ...

  5. The Standard SSL Handshake

    The following is a standard SSL handshake when RSA key exchange algorithm is used: 1.  Client Hello ...

  6. day10-函数基础知识

    函数 什么是函数 把工具事先准备好,然后下次使用的时候,直接使用就行了.我们的函数就是工具 为何用函数 1.遇到重复的功能只能重复编写实现代码,代码冗余 2.功能需要扩展时,需要找出所有实现该功能的地 ...

  7. .net core里用ZXing生成二维码

    先获取Nuget包 static void Main(string[] args) { string content = "二维码信息"; BitMatrix byteMatrix ...

  8. CAD把一个DWG文件中的多个图框一次性全部插入到打开的DWG文件中

    主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块,详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...

  9. java aop面向切面编程

    最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...

  10. ubuntu14.0开机guest账号禁用方法

    在终端里进入/usr/share/lightdm/lightdm.conf.d/目录 sudo vim 50-unity-greeter.conf 然后在文件里输入: [SeatDefaults] a ...