The below function logic works, but reading through various posts and SO questions, learned that this should be avoided. So, I'm trying to use callback in setState to reference the previous state. saveOptions function call is dependent on the state change.
Converted the below code
validateOnSave() {
const copy = this.state.codeSelectionIsInvalid;
copy[0] = !this.state.reasonCode;
copy[1] = !this.state.remarkCode;
copy[2] = !this.state.otherCode;
this.setState({ codeSelectionIsInvalid: copy });
if (!copy[0] && !copy[1] && !copy[2]) {
this.saveOptions();
}
}
To use callback
validateOnSave() {
this.setState(prevState => ({
codeSelectionIsInvalid: [!prevState.reasonCode, !prevState.remarkCode, !prevState.otherCode],
},
() => {
if(!prevState.reasonCode && !prevState.remarkCode && !prevState.otherCode) {
this.saveOptions();
}
}
)
}
Error:
{ SyntaxError: OptionsView.jsx: Unexpected token, expected "," (110:2)
108 | }
109 | )
> 110 | }
But this is not quite working. Giving me syntax errors. Where am I going wrong here? Thanks for the help!