美文网首页
Redux 去改变store中的数据

Redux 去改变store中的数据

作者: LinaXuanling | 来源:发表于2019-04-24 13:59 被阅读0次

一、创建store
index.js

import { createStore } from 'redux';
import reducer from './reducer';

const store = createStore(reducer);

export default store;

reducer.js

const defaultState = {
    inputValue: '123',
    list: [1,2]
}

export default (state = defaultState, action) => {
    return state;
}

TodoList.js

constructor(props) {
  this.state = store.getState();
}
render() {
 <div>
    <Input 
    value={this.state.inputValue} 
    placeholder="输入info" 
    style={{width: '300px', marginRight: '10px'}}
    onChange={this.handleInputChange}
    />
  <Button type="primary" onClick={this.handleButtonClick}>提交</Button>
  <List
    style={{marginTop:'10px', width:'300px'}}
    bordered 
    dataSource={this.state.list}
    renderItem={(item, index) => (<List.Item onClick={this.handleItemDelete.bind(this, index)}>{item}</List.Item>)}
    />
</div>
}

二、首先在组件中创建action, 将值传给store,再传给reducer;reducer进行数据的更改返回给store。store拿到reducer返回给它的数据后,自己进行一个更新。

handleInputChange(e) {
   const action = {
       type: 'input_change_vaule',
       value: e.target.value
   }
   store.dispatch(action);
}

handleButtonClick() {
   const action = {
       type: 'add_todo_item'
   }
   store.dispatch(action);
}

handleItemDelete(index) {
   const action = {
     type: 'delete_todo_item',
     index
   }
   store.dispatch(action);
}

reducer.js中对接收到的actiion的type来进行判断。

const defaultState = {
    inputValue: '123',
    list: [1,2]
}

//reducer 可以接受state, 但是绝不能修改state
export default (state = defaultState, action) => {
    if(action.type === 'input_change_value') {
        const newState = JSON.parse(JSON.stringify(state));
        newState.inputValue = action.value;
        return newState;
    }
    if(action.type === 'add_todo_item') {
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.push(newState.inputValue);
        newState.inputValue = '';
        return newState;
    }
    if(action.type === 'delete_todo_item') {
        const newState = JSON.parse(JSON.stringify(state));
        newState.list.splice(action.index, 1);
        return newState;
    }
    return state;
}

三、store将更改后的数据传给Component。
在constructor中使用store中的subscribe方法监控数据改变时就触发。

constructor (props) {
   super(props);
   this.state = store.getState();
   this.handleInputChange = this.handleInputChange.bind(this); 
   this.handleStoreChange = this.handleStoreChange.bind(this);
   this.handleButtonClick = this.handleButtonClick.bind(this);
   store.subscribe(this.handleStoreChange);
}
handleStoreChange() {
  this.setState(store.getState());
}

相关文章

  • Redux 去改变store中的数据

    一、创建storeindex.js reducer.js TodoList.js 二、首先在组件中创建action...

  • Redux复习

    Redux=Reducer+Flux 大致流程:(1)component需要改变数据,通过store.dispat...

  • Redux

    Redux工作流程 redux主要通过store管理react中的数据流,工作流程如下: 第一步 创建store文...

  • angular + redux

    Redux是什么?Redux是全局的,唯一的,不可改变的内存状态数据库。它有三个角色: Store, Action...

  • 如何快速理解使用redux

    使用redux的主要流程 首先 引入 redux 使用redux中的createStore去创建一个store 在...

  • 简述 Redux

    redux:解决数据传递问题,把所有数据放在store中传递。 1.基于react什么时候要用redux reac...

  • redux学习笔记

    什么是redux架构 //首先我们应该认识redux的核心store,store直译过来就是仓库,redux概念中...

  • 小程序框架wepy+redux的联合开发小程序

    redux基础 在redux整体框架中,由store统一管理系统数据,因此,需要先设计action,reducer...

  • Redux

    Redux这个npm包,提供若干API让我们使用reducer创建store,并能更新store中的数据或获取st...

  • redux的学习笔记

    Redux的设计理念:把所有的数据放到store里进行管理,一个组件改变的store里面的内容,其他的组件就感知到...

网友评论

      本文标题:Redux 去改变store中的数据

      本文链接:https://www.haomeiwen.com/subject/ovuwgqtx.html