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