【问题标题】:What is the most efficient way to transfer few props from a component to its child in ReactJS?在 ReactJS 中将少量 props 从组件传输到其子组件的最有效方法是什么?
【发布时间】:2015-06-28 03:15:16
【问题描述】:

我应该将我想要传输的所有数据连接到一个对象中并将其作为单个道具发送还是将它们中的每一个作为单独的道具单独传输?

假设,我有 10 个道具:

2 个小型 json 数组,

5 个整数

3 个字符串。

在子组件中,我将在某些函数中使用它来呈现正确的数据。 我将拥有 50 到 1000 个这样的子组件

【问题讨论】:

  • 你能提供更多的上下文吗?为什么要十个道具?它们是如何使用的?
  • 我添加了其他信息

标签: reactjs


【解决方案1】:

我认为在这种情况下最好的方法(最干净的方法)是使用spread attributes。效率上没有区别。

var props = {};
props.foo = x;
props.bar = y;
var component = <Component {...props} />;

【讨论】:

    【解决方案2】:
    var app = React.createClass({
        render() {
            var data = {
                 array1: this.props.array1,
                 array2: this.props.array2,
                 interger1: this.props.int1,
                 etc etc..
            }
            return (
                <component2 data={data} />
            )
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2016-11-10
      • 2018-07-17
      相关资源
      最近更新 更多