I am trying to push an obj into an array located in a nested object. What would be the best approach because I want it to render the component as well with the new obj.
What I want to achieve with this is for the user to be able to create new categories for the tasks
const [formElements, setFormElements] = useState({
task: {
type: 'text',
value: 'text',
className: 'task-input'
},
categories: {
type: 'select',
value: 'select',
className: 'categories-input',
cat: [
{value: 'money', displayValue: 'Money'},
{value: 'health', displayValue: 'Health'},
{value: 'daily', displayValue: 'Daily'},
{value: 'food', displayValue: 'Food'}
]
}
})
const buttonAddCategoryHandler = (e) => {
e.preventDefault()
setFormElements(prevState => {
???
})
}
I know this might be a bad question but I have a problem understanding how to update the state in a situation like this. Or this way of having a state is bad from the start?