I'm trying to build a react app that exports a DnD 5e character sheet. There are different bonuses based on your selected race/species. I have a dropdown menu to select your race and I want to apply the bonus to the appropriate stat. The problem is that if I then select a different option it doesn't revert the stats. So if I click "Orc" I get a +1 to strength, but if I change it to "Elf", I get a +1 to intelligence but the +1 to strength sticks around and going back to "Orc" adds another +1 to strength. Any advice on setting these conditional bonuses?
Here is the function that assigns the stat values
<div>
{standardArray.map((stat, index) => {
return (
<div key={index}>
<Text marginLeft={5}> {stat.name[index]}</Text>
<Select
placeholder={stat.name[index]}
width="90%"
marginLeft={3}
marginBottom={3}
onChange={handleChange}
name={"playerStat" + [index]}
>
Name: {stat.name}
{stat.value.map((value, index) => {
return (
<option key={index} value={value}>
{value}
</option>
);
})}
</Select>
</div>
);
})}
</div>
This is the dropdown menu that calls the bonus function
<Select
placeholder="Choose a Species"
color={"White"}
marginLeft={3}
marginRight={3}
width="90%"
onChange={handleSpeciesChange}
value={state.playerSpecies}
name="playerSpecies"
>
<option value="Orc">Orc</option>
<option value="Elf">Elf</option>
<option value="Human">Human</option>
</Select>
This is the function that applies the bonus
const speciesBonus = (value: string) => {
if (value === "Orc") {
const speciesBonus = 1;
state.playerStat0 = parseInt(state.playerStat0) + speciesBonus;
}
if (value === "Elf") {
const speciesBonus = 1;
state.playerStat3 = parseInt(state.playerStat3) + speciesBonus;
} else {
state.playerStat0 = parseInt(state.playerStat0);
}
};
[1]: https://i.sstatic.net/Ia3mceWk.png
[2]: https://i.sstatic.net/Yjdc48hx.png
[3]: https://i.sstatic.net/ICMR3tWk.png