在渲染容器之前执行react redux调度操作 [英] React redux dispatch action before render container

查看:200
本文介绍了在渲染容器之前执行react redux调度操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对React-redux应用程序开发很新,我试图了解如何在页面加载后立即发送另一个操作。以下是我的容器代码。我正在使用它( https://github.com/jpsierens/webpack-react-redux)样板。

I am very new to React-redux applications development and I am trying to understand how to dispatch another action as soon as the page loads. Following is my container code. I am using this (https://github.com/jpsierens/webpack-react-redux) boilerplate.

let locationSearch;

const ActivationPage = ({activateUser}) => {
    return (
        <div className="container">
          <h2>Activation Required</h2>
          <p>An Activation Email was sent to your email address. Please check your inbox to find the activation link</p>
          { activateUser() }
        </div>
    );
};


ActivationPage.propTypes = {
    activateUser: PropTypes.func
};


const mapStateToProps = (state) => {
    return {
        message: state.message,
        currentUser: state.currentUser,
        request: state.request
    };
};

const mapDispatchToProps = (dispatch) => {
    return {
        activateUser: () => {
            console.log(location);
            if (location.search !== '') {
                locationSearch = querystring.parse(location.search.replace('?', ''));
                console.log(locationSearch);
                if (locationSearch.hasOwnProperty('user_id') && locationSearch.hasOwnProperty('activation_code')) {
                    // change request state to pending to show loader
                    dispatch(actions.requestActions.requestPending());

                }
            }
        }
    };
};

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

此代码为我提供警告:setState(...):在现有状态转换期间无法更新可能是因为我在渲染功能(IDK)期间调度了一个动作。如何在页面加载后自动转换给定代码以自动触发activateUser()函数。

This Code gives me Warning: setState(…): Cannot update during an existing state transition probably because I am dispatching an action during the render function (IDK). How can I convert the given code to trigger the activateUser() function automatically as soon as the page loads.

推荐答案

https://facebook.github.io/react/docs/react-component.html

componentWillMount() componentDidMount()为了你。
和我的意见 - 你应该避免使用 componentWillMount 而更喜欢 componentDidMount - 这是有意义的你将会使用服务器渲染

componentWillMount() and componentDidMount() for you. And my opinion - you should avoid using the componentWillMount and prefer the componentDidMount - this is make sense if you somewhen will be using server rendering

这篇关于在渲染容器之前执行react redux调度操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆