Reduction reaction when sending a container

I am very new to developing React-redux applications, and I'm trying to figure out how to submit another action right after the page loads. Below is the code for my container. I am using this ( https://github.com/jpsierens/webpack-react-redux ) template.

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

This code gives me a Warning: setState (...): cannot be updated during an existing state transition , probably because I dispatch the action during the render function (IDK). How can I convert this code to activate the activateUser () function automatically as soon as the page loads.

+6
2

https://facebook.imtqy.com/react/docs/react-component.html

componentWillMount() componentDidMount() . - componentWillMount componentDidMount - , -

+4

, class, extends Component componentDidMount() -redux connect()? , , .

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import actions from '../actions';

let locationSearch;

class ActivationPage extends Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.activateUser();
    }

    render() {
        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>
        </div>
        );
    }
}

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

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

const mapDispatchToProps = (dispatch) => {
    return {
        activateUser: () => {
            if (location.search !== '') {
                locationSearch = querystring.parse(location.search.replace('?', ''));
                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);
+2

Source: https://habr.com/ru/post/1016898/


All Articles