I'm creating a react/redux app to make characters for a role playing game and I want to be able to add and remove skills from the player state.
The way I'm currently removing skills looks like this:
const initState = {
activeSkills: {},
knowledgeSkills: {},
skillPointsSpent: 0
}
const skillReducer = (state=initState, action) => {
var {category, name} = action.parameter
const actionsToTake = {
...//other actions
DECREMENT_SKILL: () => {
var newState;
if(state[category][name].rating === 1) {
newState = Object.assign(
{},
state,
{
skillPointsSpent: state.skillPointsSpent - 1
}
);
delete newState[category][name];
}
return newState
},
...//other actions
}
return (actionsToTake[action.type] || actionsToTake.DEFAULT)();
}
The state I'm reducing from should look something like this, where I'm attempting to remove Archery. So category will be activeSkills
and name will be Archery
state = {
activeSkills: {
Archery: {rating: 1, spec: 'Bow'}
},
knowledgeSkills: {},
skillPointsSpent: 1
}
This is clearly the wrong way of doing it as it mutates my previous states. So what's an immutable way of removing properties from the state object?