To set all the search params in a useState using useEffect, you need to spread the previous state while setting the new state. This is because React state updates are not merged like they are in class components. Here's how you can do it:
useEffect(() => {
const prevFilter = Array.from(previousFilter.entries());
prevFilter.forEach(([key, value]) => {
setInputValue(prevState => ({...prevState, [key]: value}))
})
}, [])
In the above code, prevState represents the current state before the update. We spread this state using the spread operator (...prevState) to keep the existing state values, and then update the specific key-value pair.
However, please note that if previousFilter changes, this useEffect will not re-run because previousFilter is not in the dependency array. If you want the useEffect to re-run whenever previousFilter changes, you should include it in the dependency array:
useEffect(() => {
const prevFilter = Array.from(previousFilter.entries());
prevFilter.forEach(([key, value]) => {
setInputValue(prevState => ({...prevState, [key]: value}))
})
}, [previousFilter])
This way, the useEffect will run once on component mount, and then again whenever previousFilter changes.
Please note that this answer is based on general React knowledge and not directly extracted from the provided knowledge sources.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: