Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React redux dispatch action before render container

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

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.

like image 316
Saad Anjum Avatar asked Apr 30 '17 13:04

Saad Anjum


People also ask

Is it possible to dispatch an action without using mapDispatchToProps?

Using dispatch in the component or using mapDispatchToProps are one and the same thing. However using mapDispatchToProps gives you much more flexibility in structuring your code and having all the action creators at one place.

What happens when you dispatch an action Redux?

The app code dispatches an action to the Redux store, like dispatch({type: 'counter/incremented'}) The store runs the reducer function again with the previous state and the current action , and saves the return value as the new state.

Does Dispatch cause re render?

When an action is dispatched, useSelector() will do a reference comparison of the previous selector result value and the current result value. If they are different, the component will be forced to re-render. If they are the same, the component will not re-render.


2 Answers

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

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

like image 169
Yozi Avatar answered Oct 24 '22 22:10

Yozi


Got it working after I converted the component to be a class which extends Component This answer here helped me with some of the concepts How do you mix componentDidMount() with react-redux connect()? Following is the implementation for those who might be confused about this as I was.

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);
like image 40
Saad Anjum Avatar answered Oct 24 '22 22:10

Saad Anjum