码迷,mamicode.com
首页 > 其他好文 > 详细

redux - mini

时间:2018-06-25 11:01:15      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:pen   const   shared   splay   ret   clu   close   return   anti   

 

1. include createStore

import { createStore } from ‘redux‘

2. Establish reducer

const reducerName = (state, action) => {
    state;
}

 

3. invoke createStore()

const store = createStore(reducerName);

 

code:

技术分享图片
import { createStore } from ‘redux‘;

const reducer = (state, action) => {
    switch (action.type) {
        case ‘ADD_RECIPE‘:
            return Object.assign(
                {}, state, {
                    recipes: state.recipes.concat({name : action.name})
            });
            
    }
    return state;
}

// https://s3.amazonaws.com/500tech-shared/db.json
 const initialState = { 
  "recipes": [
    {
      "name": "Omelette"
    },
    {
      "name": "Waffle"
    }
  ],
  "ingredients": [
    {
      "name": "Eggs",
      "quantity": 2,
      "measure": "large",
      "recipe": "Omelette"
    },
    {
      "name": "Eggs",
      "quantity": 1,
      "measure": "large",
      "recipe": "Waffle"
    },
    {
      "name": "Milk",
      "quantity": 1,
      "measure": "cups",
      "recipe": "Waffle"
    },
    {
      "name": "Sugar",
      "quantity": 2,
      "measure": "tbsp",
      "recipe": "Waffle"
    }
  ]
}

const store = createStore(reducer, initialState);

console.log(store.getState());
store.subscribe(()=>(console.log("store change")));

store.dispatch({type:‘ADD_RECIPE‘, name: ‘xiaobin‘});

console.log(store.getState());
index.js

 

db.json

技术分享图片
{
  "recipes": [
    {
      "name": "Omelette"
    },
    {
      "name": "Waffle"
    }
  ],
  "ingredients": [
    {
      "name": "Eggs",
      "quantity": 2,
      "measure": "large",
      "recipe": "Omelette"
    },
    {
      "name": "Eggs",
      "quantity": 1,
      "measure": "large",
      "recipe": "Waffle"
    },
    {
      "name": "Milk",
      "quantity": 1,
      "measure": "cups",
      "recipe": "Waffle"
    },
    {
      "name": "Sugar",
      "quantity": 2,
      "measure": "tbsp",
      "recipe": "Waffle"
    }
  ]
}
db.json

 

redux - mini

标签:pen   const   shared   splay   ret   clu   close   return   anti   

原文地址:https://www.cnblogs.com/xiaobin-hlj80/p/9222520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!