Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component not re-rendering #1560

Closed
brandonmp opened this issue Mar 31, 2016 · 5 comments
Closed

Component not re-rendering #1560

brandonmp opened this issue Mar 31, 2016 · 5 comments

Comments

@brandonmp
Copy link

I checked the docs & this issue, but none of the troubleshooting did the trick.

I have a sign-in component for user auth/account creation. I display it in a side-nav menu, so to easily navigate to the first page in registration, i have the following check in my component lifecycle:

 componentWillMount: function() {
            if (this.state.formIndex <= 1 && this.props.UIState.userMustRegister == true) {
                this.jumpToRegistration();
            }
        },
...
jumpToRegistration: function() {
            this.setState({formIndex: 2});
        },

where formIndex tells my render function which page to render.

Anywho, when I click 'Create account,' I see the correct actions fire in Redux DevTools, and my button turns to a spinner.

DevTools shows that the user has account has been created, and the user is logged-in. So, the form should move to the first page of a small questionairre, but it doesn't. I even put a console.log statement in my componentWillMount method, but it doesn't fire when the props change (ie, those tied to redux store).

While trouble-shooting, I also put a console.log in my mapStateToProps function to check when it was firing.

function mapStateToProps(state) {
    console.log("remapping:", state.UIState.userMustRegister);
    return {
        isLoggedIn: state.userAuth.authStatus,
        userProfile: state.userProfile,
        UIState: state.UIState
    }
}

image

As you can see, the value I'm monitoring in that console.log is changing from undefined to true.

So, my reducers all correctly use Object.assign(), my DevTools look good, and on refresh, everything works like a charm.

just for completeness, here's the reducer that handles the last few actions fired in my account creation logic.

//reducers/UIState.js
      case C.USER_MUST_REGISTER:
            return Object.assign({}, currentState, {userMustRegister: true});

//reducers/userProfile.js
 case C.LOAD_USER_PROFILE:
            return Object.assign({}, currentState, action.userProfile);

Where should I look next?

@brandonmp
Copy link
Author

my devtools readout. this suggests both the content and shape of my state are changing

image

@sacho
Copy link

sacho commented Mar 31, 2016

componentWillMount fires only once in a component's lifecycle. It seems like you want to also use componentWillReceiveProps.

@mxstbr
Copy link
Contributor

mxstbr commented Mar 31, 2016

As @sacho said, using componentWillReceiveProps should solve this issue!

@mxstbr mxstbr closed this as completed Mar 31, 2016
@gaearon
Copy link
Contributor

gaearon commented Mar 31, 2016

Yeah. Note that this.props is not yet updated by the time componentWillReceiveProps runs so you need to read props from its first nextProps argument.

@brandonmp
Copy link
Author

gah, my newb is showing. Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants