Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Created July 2, 2017 05:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xgqfrms-GitHub/233bff1e7c701f664f85f877a1346419 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/233bff1e7c701f664f85f877a1346419 to your computer and use it in GitHub Desktop.
redux state management react
@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jul 2, 2017

Redux Dataflow

https://gist.github.com/xgqfrms-GitHub/233bff1e7c701f664f85f877a1346419#gistcomment-2137959

basic

state -> components(event) -> Actions(action) -> Dispacther(action & prevState) -> Reducer(state) -> state

Side Effects

state -> components(event) -> Actions(action)
-> Dispacther(action & prevState) -> Reducer(state) -> state

state -> components(event) -> Actions(action)
=> MiddleWare(req) <--> API(res)
=> Dispacther(action & prevState) -> Reducer(state) -> state

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

Store

dispatch(action & prevState) & reducers(state)

getState() & subscribe(listener)

http://redux.js.org/docs/basics/Store.html

import {createStore} from 'redux';

import todoApp from './reducers';
// many reducers



let store = createStore(todoApp);




/*
It's easy to create a store if you have a reducer. 
In the previous section, we used combineReducers() to combine several reducers into one. 
We will now import it, and pass it to createStore().


如果你有一个减速器,那么很容易创建一个商店。
在上一节中,我们使用combineReducers()将几个reducer组合成一个。
我们现在将导入它,并将其传递给createStore()。
*/

let store = createStore(todoApp, window.STATE_FROM_SERVER);

/*

You may optionally specify the initial state as the second argument to createStore(). 
This is useful for hydrating the state of the client to match the state of a Redux application running on the server.


您可以选择将初始状态指定为createStore()的第二个参数。
这对于保持客户端的状态以匹配在服务器上运行的Redux应用程序的状态非常有用。

*/
import {
    addTodo,
    toggleTodo,
    setVisibilityFilter,
    VisibilityFilters
} from './actions';

// Log the initial state
console.log(store.getState());

// Every time the state changes, log it
// Note that subscribe() returns a function for unregistering the listener
let unsubscribe = store.subscribe(() =>
    console.log(store.getState());
);

// Dispatch some actions
store.dispatch(addTodo('Learn about actions'));
store.dispatch(addTodo('Learn about reducers'));
store.dispatch(addTodo('Learn about store'));
store.dispatch(toggleTodo(0));
store.dispatch(toggleTodo(1));
store.dispatch(setVisibilityFilter(VisibilityFilters.SHOW_COMPLETED));

// Stop listening to state updates
unsubscribe();

@xgqfrms-GitHub
Copy link
Author

Actions

http://redux.js.org/docs/basics/Actions.html

/*
 * action types
 */

export const ADD_TODO = 'ADD_TODO';
export const TOGGLE_TODO = 'TOGGLE_TODO';
export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER';

/*
 * other constants
 */

export const VisibilityFilters = {
    SHOW_ALL: 'SHOW_ALL',
    SHOW_COMPLETED: 'SHOW_COMPLETED',
    SHOW_ACTIVE: 'SHOW_ACTIVE'
};

/*
 * action creators
 */

export function addTodo(text) {
    return {
        type: ADD_TODO,
        text
    };
}

export function toggleTodo(index) {
    return {
        type: TOGGLE_TODO,
        index
    };
}

export function setVisibilityFilter(filter) {
    return {
        type: SET_VISIBILITY_FILTER,
        filter
    };
}

http://redux.js.org/docs/basics/DataFlow.html

http://redux.js.org/docs/basics/UsageWithReact.html

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jul 2, 2017

redux & react

https://github.com/kenberkeley/redux-simple-tutorial

https://github.com/kenberkeley/react-demo

├─ build/            # Webpack 配置目录
├─ dist/             # build 生成的生产环境下的项目
├─ src/              # 源码目录(开发都在这里进行)
   ├─ assets/         # 放置需要经由 Webpack 处理的静态文件
   ├─ components/     # 组件(COMPONENT)
   ├─ redux/          # Redux 一箩筐
      ├─ actions/      # (ACTION)
      ├─ reducers/     # (REDUCER)
      ├─ store/        # (STORE)
   ├── routes/        # 路由(ROUTE)
   ├── services/      # 服务(SERVICE,用于统一管理 XHR 请求,这是从 Vue Demo 中直接复制过来的)
   ├── utils/         # 工具库(UTIL)
      ├─ HoC/          # 高阶组件(HOC,全称 Higher Order Component)
      ├─ mixins/       # 混合(MIXIN)
   ├── views/         # 路由视图基页(VIEW)
      ├─ layout/       # 全局布局
   ├── app.js         # 启动文件
   ├── index.html     # 静态基页
├── static/          # 放置无需经由 Webpack 处理的静态文件
├── .babelrc         # Babel 转码配置
├── .eslintignore    # (配置)ESLint 检查中需忽略的文件(夹)
├── .eslintrc        # ESLint 配置
├── .gitignore       # (配置)需被 Git 忽略的文件(夹)
├── package.json     # (这个就不用多解释了吧)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment