Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Append websocket data to state in React

$
0
0

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?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>