javascript icon indicating copy to clipboard operation
javascript copied to clipboard

A question about react stateless function

Open ehrudxo opened this issue 10 years ago • 4 comments

In the link https://github.com/reactjs/redux/issues/1176

I found that stateless function is slow because of rendering, so they recommend React function even if it is stateless.

Is there any other reason you suggest stateless function?

ehrudxo avatar Jul 04 '16 00:07 ehrudxo

State is toxic and adds cognitive overhead, and it should be avoided whenever possible. SFCs aren't better because they're faster - although in the future they will be faster - they're better precisely because they're stateless.

ljharb avatar Jul 04 '16 00:07 ljharb

In addition, "performance" should always be the last concern, prioritized far after maintainability, clarity/cleanliness, elegance, and after things are tested and benchmarked. A style guide only recommends one option for performance when all options are otherwise equal - a rare occurrence.

ljharb avatar Jul 04 '16 00:07 ljharb

If you're measuring an actual perf loss due to re-renders of functional components, you can wrap them with recompose's pure, onlyUpdateForKeys, or onlyUpdateForPropTypes.

jimbolla avatar Jul 04 '16 01:07 jimbolla

@ljharb thanks a lot for your comment.

ehrudxo avatar Jul 04 '16 04:07 ehrudxo