If I have something like the following:
export default class Questions extends Component {
state = {
questions: [
{value: 'value one, required: true},
{value: 'value two, required: true},
{value: 'value two, required: true}
]
};
...
...and I want to change the middle object's required
to false
, what would be the best way? So I would end up with:
questions: [
{value: 'value one, required: true},
{value: 'value two, required: false},
{value: 'value two, required: true}
]
I tried:
_toggleRequired = (index) => {
this.setState({
questions[index].required = !questions[index].required
})
};
... but it's giving me a syntax error on the first [index]
. Isn't it also probably best to return a new array instead of mutating the existing one?
TIA!