Use state to replace complete object

Viewed 100

I have beolow code

const [data,setData] = useState(obj)

Now I am passing this data to some component

<comp data={data} />

Now When I do below

setData(newOBj);

then comp does not render again

but when I do something like changing any property it works

setData(prev=> ({
      ...prev,
      pOPageIndex: null,
      cCPageIndex: null
    }));

How do we replace complete object and make the component render again

Thanks

1 Answers

Sounds like you mutate state, in order for a component to re-render, React makes a shallow comparison with previous state / props, so having a new reference will re-render your component:

setData({...newOBj})
Related