I was learning about react hooks and I was going through the example by in the academind,
There the author has mentioned something like this
When you set a new state with React Hooks (i.e. via setCart in our example), the old state will always be replaced!
With probably following example:
import React, { useState } from 'react'
const Shop = props => {
const [cart, setCart] = useState([])
const cartHandler = () => {
setCart(['A Book'])
}
return <button onClick={cartHandler}>Add to Cart</button>
}
I am unable to comprehend this.
In class with the state, If we do setState, it also replaces the old state so what does the author try to mean here?
And later in this article, something like this
If it is an object, just keep in mind that React won’t merge your old state with your new state when you’re setting it. You will always overwrite the old state and hence any merging that might be required has to be done by you!
This all sounds very similar to the setState we do it in class baed component