I use mapStateToProps to map some value from the redux store to props:
function mapStateToProps(state, ownProps) {
// state.layers has 3 Objects containing arrays
return {
layers: state.availableLayers.toObject()
};
}
I am displaying the different layers and the user can chose the one he/she wants to create. On click event I write the chosen layer to state:
openParams(layer){
this.setState({
showLayer: layer
});
}
The argument for the layer is passed from a click event where I iterate through all the posible layers in this.props.layers.
Now I want the user to be able to change some config. For that I have a handleInput event whenever the input of a config field changes:
handleChange(e) {
let field = e.target.name;
let value = e.target.value;
let tmpLayer= this.state.showLayer;
tmpLayer[field]["value"] = value;
this.setState({ showLayer: tmpLayer });
}
When the user is done I call the redux action: this.props.actions.createLayer(this.state.showLayer);
But when I do that, the store does not only create the layer. But also changes the state.availableLayers in the redux store. But that data should stay untouched. My reducer does not change it, I already checked that. It seems like the line tmpLayer[field]["value"] = value; is changing the redux store. But I don't understand why? It seems like there is some reference from my this.state.showLayer to the redux store?! I already tried to copy the object on mapStateToProps with immutable.js and with Object.assign({}, state.availableLayer.toObject(), but it didn't help.
EDIT: using JSON.parse(JSON.stringify(this.state.showLayer)) resolves the issue while Object.assign({}, this.state.showLayer) and {...this.state.showLayer} suggested by @mkatanski do not. How that?