I have a use effect which is supposed to keep a button disabled until a row on a table has been clicked, then the button will be clickable. For some reason, it is not updating the state. I put a console log in the 'if' part of my use effect to check it was even getting to that bit and the console log prints out fine so I am not sure as to why the button will not change.
const [disabledSubmitFormButton, setDisabledSubmitFormButton] = useState(false)
const [selectedOptions, setSelectedOptions] = useState([{
name: '',
age: '',
city: '',
}])
useEffect(() => {
if (selectedOptions.length > 0) {
setDisabledSubmitFormButton(false)
console.log("hiiii")
} else {
setDisabledSubmitFormButton(true)
}
}, [selectedOptions])
And the button is listening out for the submitFormButton state like so -
<Button disabled={submitFormButton}>Submit</Button>
I have a table with rows in it, when the user select a row in the table, this is when the button should turn clickable and not be disabled. When the user clicks off the table, the button should be disabled again.
<DataTable value={optionsContext.list} header={header} sortField="id" sortOrder={-1} globalFilter={globalFilter} selectionMode="multiple"
selection={selectedOptions} onSelectionChange={e => setSelectedOptions(e.value)}>
<Column className="table-column" field="name" header="Name" sortable filter filterPlaceholder="Search by Name" />
etc.