So I have two input fields:
<TextField onChange={this.handleUserUsername.bind(this)}
value={this.props.userInfo.username}
/>
<TextField onChange={this.handleUserEmail.bind(this)}
value={this.props.userInfo.email}
/>
once entered, I would like to store them in the state of an object named 'userInfo', like so:
handleUserUsername(event){
this.props.actions.updateUsername(event.target.value)
}
handleUserEmail(event){
this.props.actions.updateEmail(event.target.value)
}
and the action creators are:
updateUsername: function(eventValue){
return {
type: 'UPDATE_USERNAME',
username: eventValue
}
},
updateEmail: function(eventValue){
return {
type: 'UPDATE_USERNAME',
email: eventValue
}
}
and the reducer is:
function(userInfo={}, action){
switch(action.type){
case 'UPDATE_USERNAME':
return {
username: action.username
}
case 'UPDATE_EMAIL':
return {
email: action.email
}
But once texts are inputted into username TextField, it displays whatever it is typed in the TextField and correctly stores 'Z' for the 'username' and logs the following (in the example, 'Z' was typed in):
Then when a text, 'M' in this example, is entered into the email TextField, the username state becomes 'undefined' when it should stay as Z, and email state is not even stored:
In the log for the example, what I am hoping to see is:
userInfo: Object
username: "Z"
email: "M"
And I am using the following to access state and actions:
function mapStateToProps(state) {
return state
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch)
}
}
Why isn't it correctly storing the state for the 'userInfo' object? What would be the proper way to update such an object? Eventually I want to store the 'userInfo' object into an array called users, where it would store all the user information.
EDIT **
When I entered 'q' in the username textfield, the following shows:
Then when I enter 'm' in the password textfield, both become undefined now:
EDIT 2 **
const registerReducer = function(userInfo={}, action){
switch(action.type){
case 'UPDATE_USERNAME':
return {
...userInfo,
username: action.username
}
case 'UPDATE_EMAIL':
return {
...userInfo,
email: action.email
}



