“React Native向useState([])追加大字符串太慢”

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

React Naitve appending large string to useState([]) takes to long

问题

我正在构建我的第一个React Native应用程序,尝试使用hooks来存储一个大的{}数组。当我尝试追加一个具有短.content的{}时,它会立即追加到数组中。然而,当.content是一个长字符串时,需要一些时间才能将其添加到数组中。我应该做些什么来改进这一点?我正在用它来制作一个聊天应用程序。使用hooks来存储消息是正确的选择,还是我应该做些其他事情?

英文:

I am building my first React Native app and I am trying to use hooks to store a large array of {}. When I try to append a {} that has a short .content it appends instantly to the array. However, when the .content is a long string it takes a while until it's added to the array. what can I do to improve that? I am using it for a chat app. Is using hooks to store the messages the right call or should I do something else?

答案1

得分: 1

当事物变得过于庞大时的技巧是将其分解。你朝着正确的方向前进。

看不到你的代码,我建议你执行以下之一:

  • 将大的 useState 拆分为多个更小的
  • 只有一个 useState,只保留你向用户显示的聊天消息
  • 只有两个 useState,第一个你只会读取(不经常向其中添加内容,依赖 React Hook 的反应性)。另一个只保留将显示给用户的聊天消息。这可能需要一些试验和逻辑来美观地管理它。

重点是只保留用户真正需要的内容。

英文:

The technique when things get too large is to break them down. You are going in the right direction.

Without seeing your code, I recommend you do one of these

  • break down large useState into multiple smaller ones
  • only have one useState that will only keep the chat message that
    you show the user
  • only have two useState, the first one you will read only(don't add
    anything to it often and depend on the reactiveness of react hook).
    Another one only keeps the chat message that you will show to the
    user. This one might need some kind of trial and error and logic to manage it beautifully.

The point is only to keep what you really need for the user.

huangapple
  • 本文由 发表于 2023年4月17日 22:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036122.html
匿名

发表评论

匿名网友

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

确定