在ReactJS中将其作为道具传递

时间:2017-06-07 16:30:14

标签: javascript reactjs

所以我有一些组件使用了很多函数,所以有时候为了避免让todo <Component func1={func1} func2={func2} ... />看起来很丑,我会在组件中调用相同的函数<Component component={this} />。所以这显然应该慢一点,但真的多少钱?这是一个糟糕的编码模式吗?我想我也可以用已使用的函数创建一个新的obj,然后将其传递给它。

编辑:我相信最好的解决方案是创建一个名为的函数: getFuncs(component) { return { func1: ..., func2: ... } }

在reactClass中然后调用:<Component funcs={this.getFuncs(this)} />

1 个答案:

答案 0 :(得分:2)

   const componentProps = {
     func1: this.func1,
     func2: this.func2
   };
   <Component {...componentProps} />