I have state object with properties as below
const initialState = {
active:false,
section: []
}
const[state, setState] = useState(initialState)
const binType = () => {
const selectedBin = []
data.map((item) => {
if(item.Bin == val){
item.title = item.Item
item.content = item.Description
selectedBin.push(item)
}
})
setState({..state,section:selectedBin})
console.log(state.section)
}
Outputs empty arrary. Any idea how to update object properties.
Top comments (4)
But if i do
I am not sure if i am doing something wrong here
Thanks for your help
Instead of trying to log it right after setState, you can try to write a useEffect, which watches for changes in state and print it there.
The problem here is, your state value is getting logged before the update in completed in your state.
Because you never update
selectedBin
variable.Also state will be updated on next re-render.
Can you please clarify what do mean by selectedBin variable is never updated.
I have updated code still same issue. Array is empty.
Thanks for your help