简体   繁体   English

如何在 JSX 中使用对象

[英]How to use an object in JSX

THIS QUESTION MIGHT NOT HAVE A CERTAIN ANSWER...这个问题可能没有确定的答案......

Which approach is better to use for using an object in JSX.哪种方法更适合在 JSX 中使用对象。

1- declare an object and use that in all usages: 1-声明一个对象并在所有用法中使用它:

            const object = GlobalState.getInstance().getObject()
            <Component1 prop1={object}></Component1>
            <Component2 prop2={object}></Component2>
            <Component3 prop3={object}></Component3>

2- pass complete reference to object we want each time we use that 2- 每次使用时传递对我们想要的对象的完整引用

                <Component1 prop1={GlobalState.getInstance().getObject()}></Component1>
                <Component2 prop2={GlobalState.getInstance().getObject()}></Component2>
                <Component3 prop3={GlobalState.getInstance().getObject()}></Component3>

which one is considered to be convention?哪一个被认为是约定俗成的? do they have any performance difference?它们有什么性能差异吗?

It's generally considered to be a good practice to:通常认为以下做法是一种很好的做法:

  1. Keep your code DRY ;保持代码干燥
  2. To store results of intermediate calculations in separate variables, so, you can document it & let other know what's that.要将中间计算的结果存储在单独的变量中,因此,您可以记录它并让其他人知道那是什么。

This way, number one wins .这样,第一名获胜 It's also better for performance.这对性能也更好。 Flawless victory!无懈可击的胜利!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM