简体   繁体   English

如何将数据异步加载到Redux?

[英]How does one load data into Redux asynchronously?

Suppose that one has the following component file HelloForm.jsx : 假设其中一个具有以下组件文件HelloForm.jsx

import React from 'react';
import {graphql} from 'react-apollo';
import {connect} from 'react-redux';
import {actions, Control, Form} from 'react-redux-form';
import {compose, withProps} from 'recompose';

import query from './HelloForm.gql';

const passThrough = fn => BaseComponent => props => {
    fn(props, BaseComponent);

    return BaseComponent(props);
};

export const HelloForm = ({onSubmitEventHandler}) =>
    <Form
            className="my-form"
            model="forms.hello"
            onSubmit={onSubmitEventHandler}
    >
        <label htmlFor="name">Name</label>
        <Control.text
            {...props}
            model=".name"
            id="name"
        />

        <button type="submit">Say Hello</button>
    </Form>;

export const enhance = compose(
    connect(),
    withProps({
        onSubmitEventHandler: ({name}) => {
            window.alert(`Hello, ${name}`);
        },
    }),
    graphql(query),
    passThrough(({data, dispatch, loading}) => {
        if (!loading) {
            dispatch(actions.load('forms.hello', data));
        }
    }),
);

export default enhance(HelloForm);

This appears to work as expected but one gets the following warning: 这似乎可以按预期工作,但是会收到以下警告:

Warning: setState(...) : Cannot update during an existing state transition (such as within render or another component's constructor). 警告: setState(...) :在现有状态转换期间(例如在render或其他组件的构造函数中)无法更新。 Render methods should be a pure function of props and state; 渲染方法应该纯粹是道具和状态的函数; constructor side-effects are an anti-pattern, but can be moved to componentWillMount . 构造函数的副作用是反模式,但是可以将其移至componentWillMount

However, React's component documentation suggests that one should dispatch the action during the componentDidMount lifecycle event (which can be done with functional components via recompose.lifecycle ). 但是, React的组件文档建议人们应该在componentDidMount生命周期事件(这可以通过recompose.lifecycle使用功能组件完成)中调度动作。 But no props are provided to the componentDidMount event handler. 但是没有提供任何道具给componentDidMount事件处理程序。

What is the proper way to "asynchronously" dispatch actions to Redux? “异步”向Redux分发操作的正确方法是什么?

The solution was indeed to use the recompose.lifecycle higher-order component. 解决方案的确是使用recompose.lifecycle高阶组件。 However, one must not use an arrow function. 但是,一定不能使用箭头功能。 The updated enchance higher-order component should be implemented as follows: 更新后的enchance高阶组件应按以下方式实现:

export const enhance = compose(
    connect(),
    withProps({
        onSubmitEventHandler: ({name}) => {
            window.alert(`Hello, ${name}`);
        },
    }),
    graphql(query),
    lifecycle({
        componentDidMount: function () {
            const {dispatch, loading, recipe} = this.props;

            if (loading) {
                dispatch(actions.load('forms.hello', data));
            }
        },
    }),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM