React支持每秒多少次useState更新?

huangapple go评论58阅读模式
英文:

how many useState updates per second react supports?

问题

I am working on an application that receives streams of market data from a websocket, (socket.io) on a react APP. This streams refers to differents stocks and I can have up to 20 streams per second.

The component that receives this streams, neets to pass them each stream to child components that shows price variation and relevant analytics for each stock

The way I found to simple share this data streams from parent to childs is to create a variable stream:

const Portfolio = () => {
...
const [message, setMessage] = useState({ticker:undefined, last_price:undefined});
useEffect(() => {
socket?.on('newstreamdata', async(data) => {
try {
setMessage(data)
} catch (err) {
console.log(err.message)
}})
}, [socket]);
...


;

The asset on the other side checks if the streams is usefull and proceeds

const Asset = (stream) => {
...
useEffect(()=>{
if (message.ticker == asset_ticker) {
setPrice(message.last_price)
}
},[message])
...
}

So far seems to work althought I am doing validations and testings. Regarding this approach my question us:

  • Will I be missing streams? I am afraid that if setUpdates runs to quick the Asset component might loose the oportunity to use a relevant stream. May be I am hitting the limit of useState updates.
英文:

I am working on an application that receives streams of market data from a websocket, (socket.io) on a react APP. This streams refers to differents stocks and I can have up to 20 streams per second.

The component that receives this streams, neets to pass them each stream to child components that shows price variation and relevant analytics for each stock

The way I found to simple share this data streams from parent to childs is to create a variable stream:

const Portfolio = () => {
...
const [message, setMessage] = useState({ticker:undefined, last_price:undefined});
useEffect(() => {
    socket?.on('newstreamdata', async(data) => {
      try {
          setMessage(data)
      } catch (err) {
          console.log(err.message)
  }})
  }, [socket]);
 ...
 <Asset message={message}>
 <Asset message={message}>
 <Asset message={message}>

The asset on the other side checks if the streams is usefull and proceeds

 const Asset = (stream) => {
 ...
     useEffect(()=>{
         if (message.ticker == asset_ticker) {
             setPrice(message.last_price)
         }
     },[message])
 ...
 }

So far seems to work althought I am doing validations and testings. Regarding this approach my question us:

  • Will I be missing streams? I am afraid that if setUpdates runs to quick the Asset component might loose the oportunity to use a relevant stream. May be I am hitting the limit of useState updates.

答案1

得分: 1

潜在问题可能出在你的 useEffect 钩子上。你写道每次 socket 变化时都会注册一个新的事件处理程序。理想情况下,你应该在 useEffect 中创建 socket 实例一次(空的依赖数组),以及注册 on 事件的事件处理程序。

英文:

One potential problem could be you useEffect hook. You wrote that a new event handler is registered on each socket change. Ideally, you create the socket instance with the useEffect once (empty dependency array) as well the event handlers for on events.

huangapple
  • 本文由 发表于 2023年5月24日 18:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定