Our Best Practices for Writing React Components
Scott Domes
1.8K84

Good stuff. We do a lot of this where I work. Question about passing functions as props…

I know it is bad to do

<MyComponent onChange={() => {doStuff}}/>

But lets say you have a class method, that takes an argument:

doStuff = (parameter) => { Do stuff with parameter }

How do I not recreate a function and use this class method?

<MyComponent onChange={this.doStuff(ARGUMENT)}/> 

The above will cause the function to fire immediately no? Is it OK in this case to do:

<MyComponent onChange={() => {this.doStuff(ARGUMENT}}/>

??

Show your support

Clapping shows how much you appreciated Tim Bell’s story.