Skip to content

Instantly share code, notes, and snippets.

@oaugusto256
Last active December 24, 2021 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oaugusto256/4934b81584fb2c79075414e0a7e1fc25 to your computer and use it in GitHub Desktop.
Save oaugusto256/4934b81584fb2c79075414e0a7e1fc25 to your computer and use it in GitHub Desktop.
Bunch of React tips to follow

List of great React tips

  1. Standardise naming. onX for eventHandler props. handleX for the func.

  2. Avoid use of anonymous functions that may cause unnecessary re-renders.

Because we are passing anonymous functions, React will always re-render since it receives a new anonymous function as a prop which it is unable to compare to the previous anonymous function (since they are both anonymous). On the other hand, passing in a reference to the method like onClick={this.handleClick} lets React know when nothing has changed so it does not unnecessarily re-render. However, the anonymous function is sometimes unavoidable as when we need to pass in an argument available only in the context: onClick={() => {this.handleClick(argHereOnly)}} .

  1. Composition and componded components over passing everything by props.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment