This is a post that tries to explain the the basics of Redux. We’ll build a minimal working example with Redux. If you’re looking for proper Redux documentation then check official docs.

What is Redux

From the official docs - Redux is a predictable state container for JavaScript. In other words Redux is meant to handle and organize application state/data.

Here is a diagram that often is confusing when you see it first time:

more diagrams here

So let’s go step by step and see what that diagram means.

State

Any application has a state. Some store their state in a database, some store their state in multiple places. In Redux you store the state in a single object. It knows which page is curently open, a set of items, current user and so on. It may be normalized or denormalized, but it should know enough so that you can save the state (say as JSON) and when loaded in a different browser - it will render the same app (same page, same items, same user…).

Let’s define our state for a counter app:

var store = mobx.observable({ counter: 0 })

Rendering

Redux works very well with React.js, but it can be rendered with anything else. We’ll render the state using plain JS:

<div id="counter">-</div>
function render(state) { document.getElementById('counter').textContent = state.counter; }

Actions

If application state changes, that’s because of actions. They could be user actions, asynchronous actions, scheduled actions and so on. We’ll define a button that will trigger an action.

<button id="button">Increment</button>
document.getElementById('button').addEventListener('click', function() { store.counter = store.counter + 1 })

Store and reducer

Actions don’t change the state directly. A Redux store is responsible for that:

Js

The Redux store holds the current state, and reacts to actions. When an action is dispatched (line 4), the store updates the state by passing current state and current action to the reducer, and the state is updated with what the reducer returned:

Js

State change

When state changes, renderer updates the render:

mobx.observe(store, function() { render(store) })

A React.js renderer is perfect in that case as it updates only what changed (and not everything as we just did).

https://bumbu.me/simple-mobx/

Simple Redux的更多相关文章

  1. [AngularJS] Write a simple Redux store in AngularJS app

    The first things we need to do is create a reducer: /** * CONSTANT * @type {string} */ export const ...

  2. Using Immutable in React + React-Redux

    React-Redux Introduction React-Redux is a library for React based on Redux package. And the core ide ...

  3. [转] How to dispatch a Redux action with a timeout?

    How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...

  4. [转] What is the point of redux when using react?

    As I am sure you have heard a bunch of times, by now, React is the V in MVC. I think you can think o ...

  5. [Redux] Extracting Container Components -- Complete

    Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...

  6. 我的前端故事----关于redux的一些思考

    背景 我一个前端,今年第一份工作就是接手一个 APP 的开发...一个线下 BD 人员用的推广 APP,为了让我这个一天原生开发都没有学过的人能快速开发上线,于是乎就选择了 react-native ...

  7. Redux thunk中间件

    redux-thunk https://github.com/reduxjs/redux-thunk Why Do I Need This? Thunks are the recommended mi ...

  8. Redux 学习总结

    1.Redux 设计理念 Web 应用是一个状态机,视图与状态是一一对应的 所有的状态,保存在一个对象里面 2.基本概念和API Redux 的核心就是 store, action, reducer ...

  9. redux超易学三篇之三(一个逻辑完整的react-redux)

    配合源代码学习吧~ : 我是源代码 这一分支讲的是 如何完整地(不包含优化,也没有好看的页面) 搭建一个 增删改查 的 react-redux 系统 不同于上一节的 react-redux,这里主要采 ...

随机推荐

  1. NPOI导出 The maximum column width for an individual cell is 255 characters

    增加如下代码 ) { arrColWidth[column.Ordinal] = ; } //设置列宽 sheet.SetColumnWidth(column.Ordinal, (arrColWidt ...

  2. Elasticsearch Transport 模块创建及启动分析

    Elasticsearch 通信模块的分析从宏观上介绍了ES Transport模块总体功能,于是就很好奇ElasticSearch是怎么把服务启动起来,以接收Client发送过来的Index索引操作 ...

  3. Jenkins教程(四)安装BlueOcean与Maven构建

    前言 本文旨在使用BlueOcean实现构建可视化与使用Maven构建上一节Jenkins教程(三)添加凭据与流水线拉取Git代码拉下来的代码 什么是Blue Ocean Blue Ocean 重新思 ...

  4. FusionInsight大数据开发---SparkStreaming概述

    SparkStreaming概述 SparkStreaming是Spark核心API的一个扩展,它对实时流式数据的处理具有可扩展性.高吞吐量.可容错性等特点. SparkStreaming原理 Spa ...

  5. Oracle学习笔记(五)

    如何查询硬解析问题: --捕获出需要使用绑定变量的SQL drop table t_bind_sql purge; create table t_bind_sql as select sql_text ...

  6. kafka Authentication using SASL/Kerberos

    Authentication using SASL/Kerberos Prerequisites KerberosIf your organization is already using a Ker ...

  7. HTML+CSS+JS综合练习(动态验证版)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. spark内存管理器--MemoryManager源码解析

    MemoryManager内存管理器 内存管理器可以说是spark内核中最重要的基础模块之一,shuffle时的排序,rdd缓存,展开内存,广播变量,Task运行结果的存储等等,凡是需要使用内存的地方 ...

  9. 好用的随查工具Dash

    1. 好用的随查工具Dash 1.1. mac版本推荐 推荐一个很好用的工具,名字就叫Dash,当然缺点就是只有英文版,中文版可能也有但更新肯定不如英文及时 可以自由下载各种文档,比如下面的这图,左侧 ...

  10. iOS UILanel 一些小实用

    UILabel *lab=[[UILabel alloc]initWithFrame:self.view.bounds]; //合并 lab.text=[NSString stringWithForm ...