Undefined value using redux












0















I'll post the code and make the question at the end.



SignUpScreen.js



export default class SignUpScreen extends Component {

constructor(props) {
super(props);
}

validateConfirmationPassword() {
const { username } = this.props;

console.log("TEST >> " + username);
}

render() {
const { username, password, email, confirmPassword } = this.props;

<Input
placeholder="Username"
value={username}
onChangeText= {(value) => this.props.onChangeText('username', value)}
/>

<Button
onPress={() => this.validateConfirmationPassword()}
/>
}


SignUpScreen_reducer.js



export default function reducer(state={
username: '',
email: '',
password: '',
confirmPassword: '',

usernameError: false,
emailError: false,
passwordError: false,
}, action) {

const { type, payload } = action

switch(type) {

case 'ON_CHANGE_UI_FIELD' : {
return {...state, [payload.key]: payload.value}
}
default: {
return state
}
}


SignUpScreenContainer.js



const mapStateToProps = (state) => {
return {
...state,
}
}

const mapDispatchToProps = (dispatch) => {
return {
onChangeText: (key, value) => {
dispatch(onChangeField(key, value))
},
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SignUpScreen);


SignUpScreen_actions.js



export function onChangeField(key, value) {
return (dispatch) => {
dispatch({type: 'ON_CHANGE_UI_FIELD', payload: {key, value}})
}
}


PS: I removed part of the code that was unecessary (like buttons and input texts). If more code is needed, please let me know.



My question is: What am I doing wrong? I keep getting 'undefined' on my console.log("TEST") for username, password, email and everything else. I have my store set up correctly. Also I can see the values being received on the actions and on the reducer, with the correct 'key' and 'value'.



Any help would be appreciated. Thanks in advance.










share|improve this question

























  • Why aren't SignUpScreenContainer.js and SignUpScreen.js in the same file?

    – Yossi
    Nov 13 '18 at 17:32











  • inside of mapStateToProps() can you please do console.log(state) and share the structure of the result?

    – Alexander Staroselsky
    Nov 13 '18 at 17:32











  • i think the problem is with 'this', as it is losing scope inside function "validateConfirmationPassword", can you console log this.props ?

    – Muhammad Sadiq
    Nov 13 '18 at 17:36






  • 1





    Your validateConfirmationPassword is unbound; either bind in the ctor or use an arrow function.

    – Dave Newton
    Nov 13 '18 at 17:37











  • @DaveNewton did that, still the same error

    – kivul
    Nov 13 '18 at 20:43
















0















I'll post the code and make the question at the end.



SignUpScreen.js



export default class SignUpScreen extends Component {

constructor(props) {
super(props);
}

validateConfirmationPassword() {
const { username } = this.props;

console.log("TEST >> " + username);
}

render() {
const { username, password, email, confirmPassword } = this.props;

<Input
placeholder="Username"
value={username}
onChangeText= {(value) => this.props.onChangeText('username', value)}
/>

<Button
onPress={() => this.validateConfirmationPassword()}
/>
}


SignUpScreen_reducer.js



export default function reducer(state={
username: '',
email: '',
password: '',
confirmPassword: '',

usernameError: false,
emailError: false,
passwordError: false,
}, action) {

const { type, payload } = action

switch(type) {

case 'ON_CHANGE_UI_FIELD' : {
return {...state, [payload.key]: payload.value}
}
default: {
return state
}
}


SignUpScreenContainer.js



const mapStateToProps = (state) => {
return {
...state,
}
}

const mapDispatchToProps = (dispatch) => {
return {
onChangeText: (key, value) => {
dispatch(onChangeField(key, value))
},
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SignUpScreen);


SignUpScreen_actions.js



export function onChangeField(key, value) {
return (dispatch) => {
dispatch({type: 'ON_CHANGE_UI_FIELD', payload: {key, value}})
}
}


PS: I removed part of the code that was unecessary (like buttons and input texts). If more code is needed, please let me know.



My question is: What am I doing wrong? I keep getting 'undefined' on my console.log("TEST") for username, password, email and everything else. I have my store set up correctly. Also I can see the values being received on the actions and on the reducer, with the correct 'key' and 'value'.



Any help would be appreciated. Thanks in advance.










share|improve this question

























  • Why aren't SignUpScreenContainer.js and SignUpScreen.js in the same file?

    – Yossi
    Nov 13 '18 at 17:32











  • inside of mapStateToProps() can you please do console.log(state) and share the structure of the result?

    – Alexander Staroselsky
    Nov 13 '18 at 17:32











  • i think the problem is with 'this', as it is losing scope inside function "validateConfirmationPassword", can you console log this.props ?

    – Muhammad Sadiq
    Nov 13 '18 at 17:36






  • 1





    Your validateConfirmationPassword is unbound; either bind in the ctor or use an arrow function.

    – Dave Newton
    Nov 13 '18 at 17:37











  • @DaveNewton did that, still the same error

    – kivul
    Nov 13 '18 at 20:43














0












0








0








I'll post the code and make the question at the end.



SignUpScreen.js



export default class SignUpScreen extends Component {

constructor(props) {
super(props);
}

validateConfirmationPassword() {
const { username } = this.props;

console.log("TEST >> " + username);
}

render() {
const { username, password, email, confirmPassword } = this.props;

<Input
placeholder="Username"
value={username}
onChangeText= {(value) => this.props.onChangeText('username', value)}
/>

<Button
onPress={() => this.validateConfirmationPassword()}
/>
}


SignUpScreen_reducer.js



export default function reducer(state={
username: '',
email: '',
password: '',
confirmPassword: '',

usernameError: false,
emailError: false,
passwordError: false,
}, action) {

const { type, payload } = action

switch(type) {

case 'ON_CHANGE_UI_FIELD' : {
return {...state, [payload.key]: payload.value}
}
default: {
return state
}
}


SignUpScreenContainer.js



const mapStateToProps = (state) => {
return {
...state,
}
}

const mapDispatchToProps = (dispatch) => {
return {
onChangeText: (key, value) => {
dispatch(onChangeField(key, value))
},
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SignUpScreen);


SignUpScreen_actions.js



export function onChangeField(key, value) {
return (dispatch) => {
dispatch({type: 'ON_CHANGE_UI_FIELD', payload: {key, value}})
}
}


PS: I removed part of the code that was unecessary (like buttons and input texts). If more code is needed, please let me know.



My question is: What am I doing wrong? I keep getting 'undefined' on my console.log("TEST") for username, password, email and everything else. I have my store set up correctly. Also I can see the values being received on the actions and on the reducer, with the correct 'key' and 'value'.



Any help would be appreciated. Thanks in advance.










share|improve this question
















I'll post the code and make the question at the end.



SignUpScreen.js



export default class SignUpScreen extends Component {

constructor(props) {
super(props);
}

validateConfirmationPassword() {
const { username } = this.props;

console.log("TEST >> " + username);
}

render() {
const { username, password, email, confirmPassword } = this.props;

<Input
placeholder="Username"
value={username}
onChangeText= {(value) => this.props.onChangeText('username', value)}
/>

<Button
onPress={() => this.validateConfirmationPassword()}
/>
}


SignUpScreen_reducer.js



export default function reducer(state={
username: '',
email: '',
password: '',
confirmPassword: '',

usernameError: false,
emailError: false,
passwordError: false,
}, action) {

const { type, payload } = action

switch(type) {

case 'ON_CHANGE_UI_FIELD' : {
return {...state, [payload.key]: payload.value}
}
default: {
return state
}
}


SignUpScreenContainer.js



const mapStateToProps = (state) => {
return {
...state,
}
}

const mapDispatchToProps = (dispatch) => {
return {
onChangeText: (key, value) => {
dispatch(onChangeField(key, value))
},
}
}

export default connect(mapStateToProps, mapDispatchToProps)(SignUpScreen);


SignUpScreen_actions.js



export function onChangeField(key, value) {
return (dispatch) => {
dispatch({type: 'ON_CHANGE_UI_FIELD', payload: {key, value}})
}
}


PS: I removed part of the code that was unecessary (like buttons and input texts). If more code is needed, please let me know.



My question is: What am I doing wrong? I keep getting 'undefined' on my console.log("TEST") for username, password, email and everything else. I have my store set up correctly. Also I can see the values being received on the actions and on the reducer, with the correct 'key' and 'value'.



Any help would be appreciated. Thanks in advance.







javascript react-native redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 18:44







kivul

















asked Nov 13 '18 at 17:29









kivulkivul

458112




458112













  • Why aren't SignUpScreenContainer.js and SignUpScreen.js in the same file?

    – Yossi
    Nov 13 '18 at 17:32











  • inside of mapStateToProps() can you please do console.log(state) and share the structure of the result?

    – Alexander Staroselsky
    Nov 13 '18 at 17:32











  • i think the problem is with 'this', as it is losing scope inside function "validateConfirmationPassword", can you console log this.props ?

    – Muhammad Sadiq
    Nov 13 '18 at 17:36






  • 1





    Your validateConfirmationPassword is unbound; either bind in the ctor or use an arrow function.

    – Dave Newton
    Nov 13 '18 at 17:37











  • @DaveNewton did that, still the same error

    – kivul
    Nov 13 '18 at 20:43



















  • Why aren't SignUpScreenContainer.js and SignUpScreen.js in the same file?

    – Yossi
    Nov 13 '18 at 17:32











  • inside of mapStateToProps() can you please do console.log(state) and share the structure of the result?

    – Alexander Staroselsky
    Nov 13 '18 at 17:32











  • i think the problem is with 'this', as it is losing scope inside function "validateConfirmationPassword", can you console log this.props ?

    – Muhammad Sadiq
    Nov 13 '18 at 17:36






  • 1





    Your validateConfirmationPassword is unbound; either bind in the ctor or use an arrow function.

    – Dave Newton
    Nov 13 '18 at 17:37











  • @DaveNewton did that, still the same error

    – kivul
    Nov 13 '18 at 20:43

















Why aren't SignUpScreenContainer.js and SignUpScreen.js in the same file?

– Yossi
Nov 13 '18 at 17:32





Why aren't SignUpScreenContainer.js and SignUpScreen.js in the same file?

– Yossi
Nov 13 '18 at 17:32













inside of mapStateToProps() can you please do console.log(state) and share the structure of the result?

– Alexander Staroselsky
Nov 13 '18 at 17:32





inside of mapStateToProps() can you please do console.log(state) and share the structure of the result?

– Alexander Staroselsky
Nov 13 '18 at 17:32













i think the problem is with 'this', as it is losing scope inside function "validateConfirmationPassword", can you console log this.props ?

– Muhammad Sadiq
Nov 13 '18 at 17:36





i think the problem is with 'this', as it is losing scope inside function "validateConfirmationPassword", can you console log this.props ?

– Muhammad Sadiq
Nov 13 '18 at 17:36




1




1





Your validateConfirmationPassword is unbound; either bind in the ctor or use an arrow function.

– Dave Newton
Nov 13 '18 at 17:37





Your validateConfirmationPassword is unbound; either bind in the ctor or use an arrow function.

– Dave Newton
Nov 13 '18 at 17:37













@DaveNewton did that, still the same error

– kivul
Nov 13 '18 at 20:43





@DaveNewton did that, still the same error

– kivul
Nov 13 '18 at 20:43












2 Answers
2






active

oldest

votes


















1














You would need to do two things to resolve this issue. First as other have mentioned you need to bind validateConfirmationPassword() in the constructor to ensure the function has the proper context when being called by onPress:



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}


Second you would need to adjust how you area attempting to access username. You indicated that when you are logging state inside mapStateToProps(), you are seeing an object such as { "signUpScreenReducer": { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, }. You are trying to access username it as const { username } = this.props;, but everything is nested within property signUpScreenReducer. You would need to access it like:



const { username } = this.props.signUpScreenReducer;


Or you could change your mapStateToProps() to Object spread the actual properties of the signUpScreenReducer (state.signUpScreenReducer) object:



const mapStateToProps = ({ signUpScreenReducer }) => {
return {
...signUpScreenReducer,
}
}


You don't even need spread, you could just also:



const mapStateToProps = ({ signUpScreenReducer }) => signUpScreenReducer;


Hopefully that helps!






share|improve this answer
























  • oh man, thank you so much, that worked!

    – kivul
    Nov 13 '18 at 21:24



















0














-Just Replace your validateConfirmationPassword function with below version.



validateConfirmationPassword = () => {
const { username } = this.props;

console.log("TEST >> " + username);
}


OR do binding for that function in constructor as below



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}





share|improve this answer


























  • @MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

    – kivul
    Nov 13 '18 at 18:39











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',
autoActivateHeartbeat: false,
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%2f53286542%2fundefined-value-using-redux%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You would need to do two things to resolve this issue. First as other have mentioned you need to bind validateConfirmationPassword() in the constructor to ensure the function has the proper context when being called by onPress:



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}


Second you would need to adjust how you area attempting to access username. You indicated that when you are logging state inside mapStateToProps(), you are seeing an object such as { "signUpScreenReducer": { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, }. You are trying to access username it as const { username } = this.props;, but everything is nested within property signUpScreenReducer. You would need to access it like:



const { username } = this.props.signUpScreenReducer;


Or you could change your mapStateToProps() to Object spread the actual properties of the signUpScreenReducer (state.signUpScreenReducer) object:



const mapStateToProps = ({ signUpScreenReducer }) => {
return {
...signUpScreenReducer,
}
}


You don't even need spread, you could just also:



const mapStateToProps = ({ signUpScreenReducer }) => signUpScreenReducer;


Hopefully that helps!






share|improve this answer
























  • oh man, thank you so much, that worked!

    – kivul
    Nov 13 '18 at 21:24
















1














You would need to do two things to resolve this issue. First as other have mentioned you need to bind validateConfirmationPassword() in the constructor to ensure the function has the proper context when being called by onPress:



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}


Second you would need to adjust how you area attempting to access username. You indicated that when you are logging state inside mapStateToProps(), you are seeing an object such as { "signUpScreenReducer": { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, }. You are trying to access username it as const { username } = this.props;, but everything is nested within property signUpScreenReducer. You would need to access it like:



const { username } = this.props.signUpScreenReducer;


Or you could change your mapStateToProps() to Object spread the actual properties of the signUpScreenReducer (state.signUpScreenReducer) object:



const mapStateToProps = ({ signUpScreenReducer }) => {
return {
...signUpScreenReducer,
}
}


You don't even need spread, you could just also:



const mapStateToProps = ({ signUpScreenReducer }) => signUpScreenReducer;


Hopefully that helps!






share|improve this answer
























  • oh man, thank you so much, that worked!

    – kivul
    Nov 13 '18 at 21:24














1












1








1







You would need to do two things to resolve this issue. First as other have mentioned you need to bind validateConfirmationPassword() in the constructor to ensure the function has the proper context when being called by onPress:



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}


Second you would need to adjust how you area attempting to access username. You indicated that when you are logging state inside mapStateToProps(), you are seeing an object such as { "signUpScreenReducer": { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, }. You are trying to access username it as const { username } = this.props;, but everything is nested within property signUpScreenReducer. You would need to access it like:



const { username } = this.props.signUpScreenReducer;


Or you could change your mapStateToProps() to Object spread the actual properties of the signUpScreenReducer (state.signUpScreenReducer) object:



const mapStateToProps = ({ signUpScreenReducer }) => {
return {
...signUpScreenReducer,
}
}


You don't even need spread, you could just also:



const mapStateToProps = ({ signUpScreenReducer }) => signUpScreenReducer;


Hopefully that helps!






share|improve this answer













You would need to do two things to resolve this issue. First as other have mentioned you need to bind validateConfirmationPassword() in the constructor to ensure the function has the proper context when being called by onPress:



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}


Second you would need to adjust how you area attempting to access username. You indicated that when you are logging state inside mapStateToProps(), you are seeing an object such as { "signUpScreenReducer": { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, }. You are trying to access username it as const { username } = this.props;, but everything is nested within property signUpScreenReducer. You would need to access it like:



const { username } = this.props.signUpScreenReducer;


Or you could change your mapStateToProps() to Object spread the actual properties of the signUpScreenReducer (state.signUpScreenReducer) object:



const mapStateToProps = ({ signUpScreenReducer }) => {
return {
...signUpScreenReducer,
}
}


You don't even need spread, you could just also:



const mapStateToProps = ({ signUpScreenReducer }) => signUpScreenReducer;


Hopefully that helps!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 21:19









Alexander StaroselskyAlexander Staroselsky

12.1k41940




12.1k41940













  • oh man, thank you so much, that worked!

    – kivul
    Nov 13 '18 at 21:24



















  • oh man, thank you so much, that worked!

    – kivul
    Nov 13 '18 at 21:24

















oh man, thank you so much, that worked!

– kivul
Nov 13 '18 at 21:24





oh man, thank you so much, that worked!

– kivul
Nov 13 '18 at 21:24













0














-Just Replace your validateConfirmationPassword function with below version.



validateConfirmationPassword = () => {
const { username } = this.props;

console.log("TEST >> " + username);
}


OR do binding for that function in constructor as below



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}





share|improve this answer


























  • @MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

    – kivul
    Nov 13 '18 at 18:39
















0














-Just Replace your validateConfirmationPassword function with below version.



validateConfirmationPassword = () => {
const { username } = this.props;

console.log("TEST >> " + username);
}


OR do binding for that function in constructor as below



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}





share|improve this answer


























  • @MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

    – kivul
    Nov 13 '18 at 18:39














0












0








0







-Just Replace your validateConfirmationPassword function with below version.



validateConfirmationPassword = () => {
const { username } = this.props;

console.log("TEST >> " + username);
}


OR do binding for that function in constructor as below



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}





share|improve this answer















-Just Replace your validateConfirmationPassword function with below version.



validateConfirmationPassword = () => {
const { username } = this.props;

console.log("TEST >> " + username);
}


OR do binding for that function in constructor as below



constructor(props) {
super(props);
this.validateConfirmationPassword = this.validateConfirmationPassword.bind(this);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 21:58









Dave Newton

139k18209254




139k18209254










answered Nov 13 '18 at 17:42









Muhammad SadiqMuhammad Sadiq

972109




972109













  • @MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

    – kivul
    Nov 13 '18 at 18:39



















  • @MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

    – kivul
    Nov 13 '18 at 18:39

















@MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

– kivul
Nov 13 '18 at 18:39





@MuhammadSadiq did both of them and neither worked. I added the log on the mapStateToProps like you asked and got this: Object { "signUpScreenReducer": Object { "email": "", "emailError": false, "password": "", "passwordError": false, "username": "", "usernameError": false, },

– kivul
Nov 13 '18 at 18:39


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286542%2fundefined-value-using-redux%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

Bressuire

Vorschmack

Quarantine