前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React第三方组件5(状态管理之Redux的使用⑤异步操作)

React第三方组件5(状态管理之Redux的使用⑤异步操作)

作者头像
前端人人
发布2018-04-11 17:05:35
1.5K0
发布2018-04-11 17:05:35
举报
文章被收录于专栏:前端人人

本教程总共6篇,每日更新一篇,请关注我们!你可以进入历史消息查看以往文章,也敬请期待我们的新文章!

1、React第三方组件5(状态管理之Redux的使用①简单使用)---2018.03.20

2、React第三方组件5(状态管理之Redux的使用②TodoList上)---2018.03.21

3、React第三方组件5(状态管理之Redux的使用③TodoList中)---2018.03.22

4、React第三方组件5(状态管理之Redux的使用④TodoList下)---2018.03.23

5、React第三方组件5(状态管理之Redux的使用⑤异步操作)---2018.03.26

6、React第三方组件5(状态管理之Redux的使用⑥Redux DevTools)---2018.03.27

开发环境:Windows 8,node v8.9.1,npm 5.5.1,WebStorm 2017.2.2

这里我们需要用到 redux-thunk

代码语言:javascript
复制
npm i -S redux-thunk

1、我们先复制一份redux4到redux5,并修改redux下Index.jsx

2、新建 store.js

代码语言:javascript
复制
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk'
import reducer from './reducer'

const middleware = [thunk];

const store = createStore(reducer, applyMiddleware(...middleware));

export default store;

3、新建action.js

代码语言:javascript
复制
import apiRequestAsync from '../../../../public/js/apiRequestAsync';

export const postList = () => async (dispatch) => {
    let todoList = await apiRequestAsync.post('todoList');
    dispatch({type: 'POST_LIST', list: todoList.list});
};

4、修改reducer.js

代码语言:javascript
复制
case 'POST_LIST':
    list = action.list;
    return {list};

完整代码

代码语言:javascript
复制
export default (state = {
    list: []
}, action) => {
    let list = state.list;
    switch (action.type) {
        case 'POST_LIST':
            list = action.list;
            return {list};
        case 'ADD':
            if (!action.title) {
                alert('不能为空');
                return state;
            }
            list.push({id: state.list.length + 1, title: action.title, status: 1});
            return {list};
        case 'EDIT':
            let {id, status} = action.obj;
            list.find(data => data.id === id).status = status;
            return {list};
        default:
            return state;
    }
};

5、修改 Index.jsx 完整代码如下

代码语言:javascript
复制
import React from 'react';
import {Provider, connect} from 'react-redux';
import store from './store'
import List from './List'
import {postList} from './action'

class Index extends React.Component {
    componentDidMount() {
        this.props.dispatch(postList());
    }

    render() {
        let props = this.props;
        return (
            <div className="todoList">
                <input type="text" ref="todoInput"/>
                <button onClick={() => this.props.dispatch({type: 'ADD', title: this.refs['todoInput'].value})}>添加
                </button>
                <div className="cont">
                    <div className="box">
                        全部
                        <List type={0} {...props}/>
                    </div>
                    <div className="box">
                        未删除
                        <List type={1} {...props}/>
                    </div>
                    <div className="box">
                        已删除
                        <List type={2} {...props}/>
                    </div>
                </div>
            </div>
        );
    }
}

const mapStateToProps = state => ({storeState: state});

const Main = connect(
    mapStateToProps
)(Index);

export default () =>
    <Provider store={store}>
        <Main/>
    </Provider>
;

6、查看浏览器

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-03-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端人人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档