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.
react-native redux parse-server
add a comment |
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.
react-native redux parse-server
Can you show all the init.js code?
– Goose
Nov 19 at 1:42
add a comment |
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.
react-native redux parse-server
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
react-native redux parse-server
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
add a comment |
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
add a comment |
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.
thank you, that worked!
– kivul
Nov 19 at 9:11
add a comment |
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.
thank you, that worked!
– kivul
Nov 19 at 9:11
add a comment |
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.
thank you, that worked!
– kivul
Nov 19 at 9:11
add a comment |
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.
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.
answered Nov 19 at 1:48
Shawn Andrews
920416
920416
thank you, that worked!
– kivul
Nov 19 at 9:11
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Can you show all the init.js code?
– Goose
Nov 19 at 1:42