Ferus
Ferus

Reputation: 1118

Passing this as prop in ReactJS

So I have some components that use a lot of functions so sometimes to avoid having todo <Component func1={func1} func2={func2} ... /> which looks ugly I do <Component component={this} /> when the same functions are called in the component. So this should obviously be slower but how much really? Is this a terrible coding-pattern? I suppose I could also just create a new obj with the used functions and pass that in instead.

EDIT: I belive the best solution is to create a function called: getFuncs(component) { return { func1: ..., func2: ... } }

In the reactClass and then call: <Component funcs={this.getFuncs(this)} />

Upvotes: 0

Views: 45

Answers (1)

vic
vic

Reputation: 96

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

Upvotes: 2

Related Questions