reactjs - React Redux 如何分派(dispatch)异步操作和更新状态

标签 reactjs react-redux dispatch redux-thunk

我尝试在我的学习 react ,redux 项目中处理 ajax 数据,但我不知道如何分派(dispatch)一个 Action 并设置组件内的状态

这是我的组件

import React, {PropTypes, Component} from 'react';
import Upload from './Upload';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import * as profileActions from '../../../actions/profileActions';


class Profile extends Component {

    static propTypes = {
        //getProfile: PropTypes.func.isRequired,
        //profile: PropTypes.object.isRequired,
    };

    constructor(props) {
        super(props);

        this.state = {
            profile:{
                username: '',
                password: ''
            }
        }
        this.onUpdate = this.onUpdate.bind(this)
    }

    onUpdate(event) {
        alert()
    }

    componentWillMount() {
    }

    componentDidMount() {
    }


    render() {

        const {profile} = this.props;
        return (
            <div>

            </div>
        );
    }
}

function mapStateToProps(state) {
    console.log(state)
    return {
        profile: state.default.profile,
    };
}

function mapDispatchToProps(dispatch, ownProps) {
    return {
        actions: bindActionCreators(profileActions, dispatch)
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(Profile);

我按照下面的方式创建商店

import { createStore, combineReducers, applyMiddleware } from 'redux'
import createLogger from 'redux-logger'
import thunk from 'redux-thunk'
import { routerReducer, routerMiddleware, push } from 'react-router-redux'
import reducers from '../reducers'
import { browserHistory } from 'react-router';

const middleware = [ thunk ];
if (process.env.NODE_ENV !== 'production') {
    middleware.push(createLogger());
}

middleware.push(routerMiddleware(browserHistory));

// Add the reducer to your store on the `routing` key
const store = createStore(
    combineReducers({
        reducers,
        routing: routerReducer
    }),
    applyMiddleware(...middleware),

)

export default store;

reducer

export const RESOLVED_GET_PROFILE = 'RESOLVED_GET_PROFILE'

const profileReducer = (state = {}, action) => {
    switch (action.type) {
        case 'RESOLVED_GET_PROFILE':
            return action.data;
        default:
            return state;
    }
};

export default profileReducer;

Action

import * as types from './actionTypes';
import Api from '../middleware/Api';

export function getProfile() {
    return dispatch => {
        dispatch(setLoadingProfileState()); // Show a loading spinner
        Api.getAll('profile').then(profile => {
            dispatch(doneFetchingProfile(profile));
        }).catch(error => {
            throw(error);
        });
        /*Api.fetch(`profile`, (response) => {
            console.log(response)
            dispatch(doneFetchingBook()); // Hide loading spinner
            if(response.status == 200){
                dispatch(setProfile(response.json)); // Use a normal function to set the received state
            }else {
                dispatch(error)
            }
        }) */
    }
}

function setProfile(data) {
    return {type: types.SET_PROFILE, data: data}
    //return { type: types.SET_PROFILE, data: data };
}


function setLoadingProfileState() {
    return {type: types.SHOW_SPINNER}
}

function doneFetchingProfile(data) {
    console.log(data)
    return {
        type: types.HIDE_SPINNER,
        profile: data
    }
}

function error() {
    return {type: types.SHOW_ERROR}
}

但我不知道如何在 getProfile 操作后发送操作和更新状态

最佳答案

您只需在发送 doneFetchingProfile 后立即发送您的事件 RESOLVED_GET_PROFILE,或者只需监听 RESOLVED_GET_PROFILE 并在减少它时隐藏微调器。

 Api.getAll('profile').then(profile => {
    dispatch(doneFetchingProfile(profile));
    dispatch(resoloveGetProfile(profile));
 })

实际上你做的一切都是对的 - 所以我不明白你的问题是什么,所以如果你的意思是别的 - 让我知道,我会试着描述你。

关于调度(resoloveGetProfile(profile));

这里是调度 Action ,它会更新你的状态,就像你对一些静态状态所做的那样简单,我看到你已经有了 setProfile Action ,所以你可以改变那一行,调用你现有的功能。

dispatch(setProfile(profile))

比你需要减少你在这个 Action 中的状态

case 'SET_PROFILE' : (action, state) => {...state, profile: action.data}

然后你的状态会改变,你的组件也会更新。请注意,您的“获取配置文件方法”最好从 componentDidMount 调用,以避免因执行网络请求而在呈现时卡住。

关于reactjs - React Redux 如何分派(dispatch)异步操作和更新状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328690/

相关文章:

reactjs - redux hooks : "could not find react-redux context value; please ensure the component is wrapped in a <Provider>" 的 enzyme 测试错误

javascript - 为什么在 onClick 之后我的钩子(Hook)无法更新状态?

javascript - 我可以在 connect redux 中传递多个组件吗

gpu - Numba 中的组合矢量化函数

ios - 如何减慢程序向服务器发出请求?

typescript - 将 react-router 与 typescript 一起使用

node.js - 如何更改客户端服务器的代理

javascript - Prop 更新太慢如何处理?

javascript - react native useSelector 不刷新

sockets - 在 socket.on() 的回调上调度操作