Redux state undefined on function but not on render()











up vote
0
down vote

favorite












Im using redux and i want to render my main drawer based if an user is logged on or not. I have this 'init' class that will check for that and do it accordingly.



I'll post the code and explain it at the end:



Init.js



const mapStateToProps = (userReducer) => {
return {
...userReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
getCurrentUser: () => {
dispatch(getCurrentUser())
}
}
}

class Init extends Component {
constructor(props) {
super(props);

this.props.getCurrentUser()

console.log("TEST>>")
console.log(this.props)
}

chooseInitialRoute() {
const { navigation, user } = this.props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


user_actions.js



export function getCurrentUser() {
return (dispatch) => {
Parse.User.currentAsync().then(function (user) {
if (user) {
dispatch({ type: 'GET_USER', payload: { user } })
} else {
dispatch({ type: 'GET_USER_REJECTED', payload: error })
}
})
}
}


The console.log(this.props) returns undefined if called inside the constructor (same happens when I do console.log(props). But if I call it inside render() I get the props correcly (meaning that I get the correct user object from parse server).



Is there any way to update the props before the render() method? I want to get the 'updated' props on the chooseInitialRoute()



Thanks in advance. If more code is needed let me know.










share|improve this question
























  • Can you show all the init.js code?
    – Goose
    Nov 19 at 1:42















up vote
0
down vote

favorite












Im using redux and i want to render my main drawer based if an user is logged on or not. I have this 'init' class that will check for that and do it accordingly.



I'll post the code and explain it at the end:



Init.js



const mapStateToProps = (userReducer) => {
return {
...userReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
getCurrentUser: () => {
dispatch(getCurrentUser())
}
}
}

class Init extends Component {
constructor(props) {
super(props);

this.props.getCurrentUser()

console.log("TEST>>")
console.log(this.props)
}

chooseInitialRoute() {
const { navigation, user } = this.props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


user_actions.js



export function getCurrentUser() {
return (dispatch) => {
Parse.User.currentAsync().then(function (user) {
if (user) {
dispatch({ type: 'GET_USER', payload: { user } })
} else {
dispatch({ type: 'GET_USER_REJECTED', payload: error })
}
})
}
}


The console.log(this.props) returns undefined if called inside the constructor (same happens when I do console.log(props). But if I call it inside render() I get the props correcly (meaning that I get the correct user object from parse server).



Is there any way to update the props before the render() method? I want to get the 'updated' props on the chooseInitialRoute()



Thanks in advance. If more code is needed let me know.










share|improve this question
























  • Can you show all the init.js code?
    – Goose
    Nov 19 at 1:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Im using redux and i want to render my main drawer based if an user is logged on or not. I have this 'init' class that will check for that and do it accordingly.



I'll post the code and explain it at the end:



Init.js



const mapStateToProps = (userReducer) => {
return {
...userReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
getCurrentUser: () => {
dispatch(getCurrentUser())
}
}
}

class Init extends Component {
constructor(props) {
super(props);

this.props.getCurrentUser()

console.log("TEST>>")
console.log(this.props)
}

chooseInitialRoute() {
const { navigation, user } = this.props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


user_actions.js



export function getCurrentUser() {
return (dispatch) => {
Parse.User.currentAsync().then(function (user) {
if (user) {
dispatch({ type: 'GET_USER', payload: { user } })
} else {
dispatch({ type: 'GET_USER_REJECTED', payload: error })
}
})
}
}


The console.log(this.props) returns undefined if called inside the constructor (same happens when I do console.log(props). But if I call it inside render() I get the props correcly (meaning that I get the correct user object from parse server).



Is there any way to update the props before the render() method? I want to get the 'updated' props on the chooseInitialRoute()



Thanks in advance. If more code is needed let me know.










share|improve this question















Im using redux and i want to render my main drawer based if an user is logged on or not. I have this 'init' class that will check for that and do it accordingly.



I'll post the code and explain it at the end:



Init.js



const mapStateToProps = (userReducer) => {
return {
...userReducer,
}
}
const mapDispatchToProps = (dispatch) => {
return {
dispatch,
getCurrentUser: () => {
dispatch(getCurrentUser())
}
}
}

class Init extends Component {
constructor(props) {
super(props);

this.props.getCurrentUser()

console.log("TEST>>")
console.log(this.props)
}

chooseInitialRoute() {
const { navigation, user } = this.props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


user_actions.js



export function getCurrentUser() {
return (dispatch) => {
Parse.User.currentAsync().then(function (user) {
if (user) {
dispatch({ type: 'GET_USER', payload: { user } })
} else {
dispatch({ type: 'GET_USER_REJECTED', payload: error })
}
})
}
}


The console.log(this.props) returns undefined if called inside the constructor (same happens when I do console.log(props). But if I call it inside render() I get the props correcly (meaning that I get the correct user object from parse server).



Is there any way to update the props before the render() method? I want to get the 'updated' props on the chooseInitialRoute()



Thanks in advance. If more code is needed let me know.







react-native redux parse-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 0:24

























asked Nov 18 at 22:30









kivul

22211




22211












  • Can you show all the init.js code?
    – Goose
    Nov 19 at 1:42


















  • Can you show all the init.js code?
    – Goose
    Nov 19 at 1:42
















Can you show all the init.js code?
– Goose
Nov 19 at 1:42




Can you show all the init.js code?
– Goose
Nov 19 at 1:42












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If console.log(props) is undefined in the constructor this means the component later gets the props via componentWillReceiveProps and i dont know where you call InitialRoute but you might consider putting it in the lifecycle function instead:



componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


Note that if your props are undefined in the constructor then your component may render multiple times before your component finally receives props via componentWillReceiveProps.






share|improve this answer





















  • thank you, that worked!
    – kivul
    Nov 19 at 9:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366090%2fredux-state-undefined-on-function-but-not-on-render%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










If console.log(props) is undefined in the constructor this means the component later gets the props via componentWillReceiveProps and i dont know where you call InitialRoute but you might consider putting it in the lifecycle function instead:



componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


Note that if your props are undefined in the constructor then your component may render multiple times before your component finally receives props via componentWillReceiveProps.






share|improve this answer





















  • thank you, that worked!
    – kivul
    Nov 19 at 9:11















up vote
0
down vote



accepted










If console.log(props) is undefined in the constructor this means the component later gets the props via componentWillReceiveProps and i dont know where you call InitialRoute but you might consider putting it in the lifecycle function instead:



componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


Note that if your props are undefined in the constructor then your component may render multiple times before your component finally receives props via componentWillReceiveProps.






share|improve this answer





















  • thank you, that worked!
    – kivul
    Nov 19 at 9:11













up vote
0
down vote



accepted







up vote
0
down vote



accepted






If console.log(props) is undefined in the constructor this means the component later gets the props via componentWillReceiveProps and i dont know where you call InitialRoute but you might consider putting it in the lifecycle function instead:



componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


Note that if your props are undefined in the constructor then your component may render multiple times before your component finally receives props via componentWillReceiveProps.






share|improve this answer












If console.log(props) is undefined in the constructor this means the component later gets the props via componentWillReceiveProps and i dont know where you call InitialRoute but you might consider putting it in the lifecycle function instead:



componentWillReceiveProps(props) {
const { navigation, user } = props;
if (user) {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'LoggedDrawer' })] }
))
} else {
navigation.dispatch(StackActions.reset(
{ index: 0, key: null, actions: [NavigationActions.navigate({ routeName: 'UnloggedDrawer' })] }
))
}
}


Note that if your props are undefined in the constructor then your component may render multiple times before your component finally receives props via componentWillReceiveProps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 1:48









Shawn Andrews

920416




920416












  • thank you, that worked!
    – kivul
    Nov 19 at 9:11


















  • thank you, that worked!
    – kivul
    Nov 19 at 9:11
















thank you, that worked!
– kivul
Nov 19 at 9:11




thank you, that worked!
– kivul
Nov 19 at 9:11


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366090%2fredux-state-undefined-on-function-but-not-on-render%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?