I am trying to create a timeseries plot that displays data collected from a websocket.
However, when I get new values, they are overwriting the previous one stored in the state.
const [chartData, setChartData] = React.useState(null)
My websocket uses setChartData() but this causes me to lose the previous value.
I have a socket that takes setChartData as an argument and on receiving a message calls setChartData(decodedData)
const Socket = ({url, protocol, dataFunction}) => { const ws = new Websocket(url); ws.onmessage = function onMessage(msg) { if (protocol === JSON) { const decoded = JSON.parse(msg.data) dataFunction(decoded) } }}
How do I go about adding these new values to a state array, rather than just the most recent value?