[React] Refactor a connected Redux component to use Unstated
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>
- );
[React] Refactor a connected Redux component to use Unstated的更多相关文章
- [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 ...
- 如何在非 React 项目中使用 Redux
本文作者:胡子大哈 原文链接:https://scriptoj.com/topic/178/如何在非-react-项目中使用-redux 转载请注明出处,保留原文链接和作者信息. 目录 1.前言 2. ...
- 【前端】react学习阶段总结,学习react、react-router与redux的这些事儿
前言 借用阮一峰的一句话:真正学会 React 是一个漫长的过程. 这句话在我接触react深入以后,更有感触了.整个react体系都是全新的,最初做简单的应用,仅仅使用react-tools打包js ...
- 如何优雅地在React项目中使用Redux
前言 或许你当前的项目还没有到应用Redux的程度,但提前了解一下也没有坏处,本文不会安利大家使用Redux 概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与 ...
- 优雅的在React项目中使用Redux
概念 首先我们会用到哪些框架和工具呢? React UI框架 Redux 状态管理工具,与React没有任何关系,其他UI框架也可以使用Redux react-redux React插件,作用:方便在 ...
- 前端笔记之React(五)Redux深入浅出
一.Redux整体感知 Redux是JavaScript状态管理容器,提供了可被预测状态的状态管理容器.来自于Flux思想,Facebook基于Flux思想,在2015年推出Redux库. 中文网站: ...
- react,react-router,redux+react-redux 构建一个React Demo
创建初始化应用 加速我们的npm. npm install -g cnpm --registry=https://registry.npm.taobao.org 利用create-react-app ...
- react、react-router、redux 也许是最佳小实践1
小前言 这是一个小小的有关react的小例子,希望通过一个小例子,可以让新手更好的了解到react.react-router4.0.redux的集中使用方法. 这是基于create-react-app ...
- 【转】浅谈React、Flux 与 Redux
本文转自<浅谈React.Flux 与 Redux>,转载请注明出处. React React 是一个 View 层的框架,用来渲染视图,它主要做几件事情: 组件化 利用 props 形成 ...
随机推荐
- scala 变量定义,基本操作符
scala是jvm语言,它将面向对象与函数风格结合的很好,它可以访问任何java类库并很好的结合使用. 它可以使程序更简单,同时可利用并发的威力. scala基本语法: package com.tes ...
- React容器组件和展示组件
Presentational and Container Components 展示组件 - 只关心它们的样子. - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式. ...
- Angular——表单指令
基本介绍 这些指定只能针对input标签 基本使用 <!DOCTYPE html> <html lang="en"> <head> <me ...
- 六时出行 App 隐私政策
六时出行 App 隐私政策 本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义 ...
- The Standard SSL Handshake
The following is a standard SSL handshake when RSA key exchange algorithm is used: 1. Client Hello ...
- day10-函数基础知识
函数 什么是函数 把工具事先准备好,然后下次使用的时候,直接使用就行了.我们的函数就是工具 为何用函数 1.遇到重复的功能只能重复编写实现代码,代码冗余 2.功能需要扩展时,需要找出所有实现该功能的地 ...
- .net core里用ZXing生成二维码
先获取Nuget包 static void Main(string[] args) { string content = "二维码信息"; BitMatrix byteMatrix ...
- CAD把一个DWG文件中的多个图框一次性全部插入到打开的DWG文件中
主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块,详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
- ubuntu14.0开机guest账号禁用方法
在终端里进入/usr/share/lightdm/lightdm.conf.d/目录 sudo vim 50-unity-greeter.conf 然后在文件里输入: [SeatDefaults] a ...