palantir / tslint-react

:orange_book: Lint rules related to React & JSX for TSLint.
Apache License 2.0
749 stars 76 forks source link

New Rule: Do not allow generation of new objects in props #89

Closed benbayard closed 5 years ago

benbayard commented 7 years ago

Similar to jsx-no-lambda, if in a prop you do:

<MyComponent 
  style={{display: 'block'}}
/>

this will force any pure component to re-render every time.

Ideally, we could check in render to see if any new objects are generated there. Such as:

render() {
  const style = {display: 'block'};

  return <MyComponent 
    style={style}
  />
}

but that could be a separate rule.

IllusionMH commented 7 years ago

Had similar thought during jsx-no-lambda rewrite.

More ideal case described above - move style variable outside of component. 😃

Also I'd like to suggest further improvements for both rule proposed in this issue as well as for jsx-no-lambda:

See React.js pure render performance anti-pattern for the reference.

benbayard commented 7 years ago

Another thing to consider would be:

<MyComponent myProp={condition ? realValue : {}} />
adidahiya commented 5 years ago

Closing due to deprecation timeline, see #210