简体   繁体   English

使用 React 上下文而不是将相同的对象传递给数十个组件是否更好?

[英]Is it better to use React context instead of passing the same object to dozens of components?

I'm adding user comments in my website and I need to pass user object to each user comment.我在我的网站中添加用户评论,我需要将用户对象传递给每个用户评论。 I have a React component called UserReplyForm which handles submit of new comment.我有一个名为UserReplyForm的 React 组件,它处理新评论的提交。 A user can reply to each comment so visually there's a component to display a comment and a form below each comment to reply.用户可以回复每条评论,因此在视觉上有一个组件来显示评论和每个评论下方的表单以供回复。

The user object is stored in a React context.用户对象存储在 React 上下文中。 Because I will have dozens of replies and potentially dozens of replies to replies I'm wondering if it's better in terms of memory optimization to get the user object via React context because if I pass it as props, the user object will be copied as many times as there're replies.因为我将有几十个回复,并且可能有几十个回复我想知道通过 React 上下文获取用户对象在内存优​​化方面是否更好,因为如果我将它作为道具传递,用户对象将被复制尽可能多有回复的时候。 When I say copied I mean that function parameters are copied by value in Javascript.当我说复制时,我的意思是在 Javascript 中按值复制函数参数。 On the other hand getting the user object from the user context I think I will just get the reference to the object.另一方面,从用户上下文中获取用户对象,我想我只会获取对对象的引用。

EDIT: come to think of it, using context doesn't really help because if I use context I will save the context result in a local variable which also uses memory.编辑:想想看,使用上下文并没有真正的帮助,因为如果我使用上下文,我会将上下文结果保存在一个也使用内存的局部变量中。 So props and context result in same memory usage.所以道具和上下文导致相同的内存使用。

当然,这就是上下文的目的。

function's passed by reference and .its parameters are also passed by reference (if it's object,array,..) and was linked to the function pointer.When function's called, it will get value of its parameters through the linked pointer.函数通过引用传递,.它的参数也是通过引用传递(如果是对象,数组,..)并链接到函数指针。当函数被调用时,它将通过链接指针获取其参数的值。 It's just my thinking, feel free to correct.这只是我的想法,欢迎指正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应组件,使用函数代替内联块事件处理程序更好吗? - React components, is it better to use functions instead inline block event handlers? 无法在深层后代组件中使用 React Context - Unable to use React Context in deep descendant components 使用 React Navigator 和 expo 将导航器 object 传递给组件 - Passing navigator object to components with React Navigator and expo 在同级组件之间传递对象-React JS - Passing an Object between sibling components - React js 使用上下文在反应中调用 Api 后将数组传递给其他组件 - Passing array to other components after calling Api in react using context 对象作为 React 子对象无效(找到:带有键 {..} 的对象)。 ...改用数组。 在 Select(由 Context.Consumer 创建) - Objects are not valid as a React child (found: object with keys {..}). …use an array instead. in Select (created by Context.Consumer) React context api 仅呈现使用已更改值的组件 - React context api only render components that use values that have changed 使用 React.Context 注入 UI 组件是个好主意吗? - Is it a good idea to use React.Context to inject UI-Components? React native - 3 个组件在同一行而不是垂直堆叠 - React native - 3 components on the same row instead of vertically stacked 将参数传递给React组件 - passing arguments into React components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM