英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论