Reputation: 1389
Can someone tell me why state.set is resulting in undefined.
I have a reducer like so:
const initialState = fromJS({
description: 'default_description',
});
function helpReducer(state = initialState, action) {
switch (action.type) {
case DEFAULT_ACTION:
return state;
case CHANGE_DESCRIPTION:
console.log('Change Description reducer action.data: '+action.data); //results in string say foo
console.log('state: '+state); //results in valid state on first call
return state
.set('description':action.data);
default:
return state;
}
}
The first time I call the action CHANGE_DESCRIPTION, state = default_description
.
After calling state.set
the state.description = undefined
.
I have used state.set before, but am unsure why it is failing now.
The action is being called by a child component like so:
Parent:
export class FOO extends React.PureComponent {
constructor(props){
super(props);
}
render() {
return (
<div>
<FOOForm onChange={this.props.changeDescription} />
</div>
);
}
}
function mapDispatchToProps(dispatch) {
return {
dispatch,
changeDescription: (e) => dispatch(changeDescription(e.target.value))
};
}
Foo Form for sake of demonstration:
function fooForm(props){
return <FormControl onChange={props.onChange} componentClass="textarea" />;
}
Upvotes: 2
Views: 1369
Reputation: 5450
I'm not sure of the context in which you implement your reducer, but I believe the syntax in immutable JS should make your set look like this:
return state.set('description', action.data)
Note that you need to pass in the property and values as arguments and not as a key value pair.
Upvotes: 1