0

I'm trying to implement a simple history of message in React.

I want to use a useEffect Hook that will depend on the sentMessage state. Upon trigger of the effect, I want to append to my history of messages the new value of the sent message.

However, using setHistoryState((prevValue) => prevValue.push(sentMessage) is, if I understand well, not the right way to proceed since historyState is not in the dependencies.

What is the correct way to implement this ?

4 Answers 4

2

Is not necessary to have historyState in useEffect's dependencies to call setHistoryState. So I think you could do something like:

useEffect(() => {
   setHistoryState(prevValue => [...prevValue, sentMessage]);
}, [sentMessage]);

This means that, every time sentMessage changes his value, new value will be pushed in historyState.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, but I thought that using setHistoryState(prevValue => [...prevValue, sentMessage]); was implicitly using the historyState value
@aurelien_morel no it isn't (if was as you thought, you should get a warning like "missing dependencies on useEffect blablabla")
1

From what i see your problem may come frome the fact prevValue.push will mutate your state "prevValue". This is not a recomanded way to manipulate states.

You could change your state this way =>

setHistoryState((prevValue) => ([...prevValue, sentMessage]))

Have a look to "avoid+mutations+js" on google.

Comments

1

You could spread the existing history and create a new array with its values, adding a new element in the process.

// historyState is an array here
const [historyState, setHistoryState] = useState([1,2,3])

setHistoryState(history => [...history, newValue]);

https://codesandbox.io/s/cocky-browser-v8jp0

5 Comments

isn't it a problem if I do this inside a useEffect() hook where historyState is not in the dependencies ?
Maybe you need to add some more context to your question
thank you for your codesandbox,, here you achieve it without the useEffect (directly using the onClick) but I can't do this since there are different historyState's coming from different children so I need to use useEffect to react to changes in each of them
Why can't you set this state within a useEffect? I don't understand that part.
because I thought that setHistoryState(history => [...history, newValue]); was using historyState as a dependency, and I can not place it in the dependency list, but it seems that it works actually
0

You make a copy of its like that

setHistoryState((prevValue) => {
    const value = JSON.parse(JSON.stringiy(prevValue) 
    value.push(sentMessage)
    return value
}

You can try another deep copy ways instead using JSON

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.