diff --git a/STYLE.md b/STYLE.md index 7a9fe40deed97..d8a5616a174b4 100644 --- a/STYLE.md +++ b/STYLE.md @@ -511,6 +511,48 @@ const propTypes = { } ``` +## Binding methods + +For class components, methods should be bound in the `constructor()` if passed directly as a prop or needing to be accessed via the component instance from outside of the component's execution context. Binding all methods in the constructor is unnecessary. Learn and understand how `this` works [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this). + +```javascript +// Bad +class SomeComponent { + constructor(props) { + super(props); + + this.myMethod = this.myMethod.bind(this); + } + + myMethod() {...} + + render() { + return ( + // No need to bind this since arrow function is used +