actionCreators统一创建action

一、在store下新建actionCreators.js

import { CHANGE_INPUT_VALUE, ADD_TODO_ITEM, DELETE_TODO_ITEM } from './actionTypes';

export const getChangeInputValue = (value) => ({
    type: CHANGE_INPUT_VALUE,
    value
})

export const getAddTodoItem = () => ({
    type: ADD_TODO_ITEM,
})

export const getDeleteTodoItem = (index) => ({
    type: DELETE_TODO_ITEM,
    index
})

TodoList.js中引用actionCreators.js

import { getChangeInputValue, getAddTodoItem, getDeleteTodoItem } from './store/actionCreators';

handleInputChange(e) {
    const action = getChangeInputValue(e.target.value);
    store.dispatch(action);
}

完整的代码:

import React, { Component } from 'react';
import 'antd/dist/antd.css';
import { Input, Button, List } from 'antd';
import store from './store';
import { getChangeInputValue, getAddTodoItem, getDeleteTodoItem } from './store/actionCreators';

class TodoList extends Component {

    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);
    }

    render () {
        return (
            <div style={{marginTop:'10px', marginLeft:'10px'}}>
                <div>
                    <Input
                        value={this.state.inputValue}
                        placeholder="输入info"
                        style={{width: '300px', marginRight: '10px'}}
                        onChange={this.handleInputChange}
                    />
                    <Button type="primary" onClick={this.handleButtonClick}>提交</Button>
                </div>
                <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>
        )
    }

    handleInputChange(e) {
        const action = getChangeInputValue(e.target.value);
        store.dispatch(action);
    }

    handleButtonClick() {
        const action = getAddTodoItem();
        store.dispatch(action);
    }

    handleStoreChange() {
        this.setState(store.getState());
    }

    handleItemDelete(index) {
        const action = getDeleteTodoItem(index);
        store.dispatch(action);
    }
}

export default TodoList;

actionTypes.js:

export const CHANGE_INPUT_VALUE = 'change_input_value';
export const ADD_TODO_ITEM = 'add_todo_item';
export const DELETE_TODO_ITEM = 'delete_todo_item';

reducer.js:

import { CHANGE_INPUT_VALUE, ADD_TODO_ITEM, DELETE_TODO_ITEM } from './actionTypes';

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

//reducer 可以接受state, 但是绝不能修改state
export default (state = defaultState, action) => {
    if(action.type === CHANGE_INPUT_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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、CMS管理系统功能 CMS是ContentManagementSystem的缩写,意为"内容管理系统"。 CM...
    默默先生Alec阅读 10,836评论 0 7
  • 看到这篇文章build an image gallery using redux saga,觉得写的不错,长短也适...
    smartphp阅读 11,387评论 1 29
  • 以前也想过走遍祖国的大好河山,了解过四川省是个好地方,但是从没想过今天置身于此处,竟是如此的心境。 以前隶属于绵阳...
    特有劲阅读 1,540评论 0 1
  • 《资治通鉴》中《胡亥为帝》详细讲述了帝国秦国始皇至二世时秦帝国的自掘坟墓过程:将不合理的合理化、派系勾心斗角、社会...
    里里溪主笔阅读 1,742评论 0 0
  • 我们任何时候都可以学一些东西,可以随心所欲地画,可以随心所欲地唱几句,随心所欲地玩一下下小乐器。
    女巫瑟西阅读 1,355评论 2 0

友情链接更多精彩内容