TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

React.js pure render performance anti-pattern

53 pointsby mxstbrover 9 years ago

8 comments

cloverichover 9 years ago
(tl;dr) The title is getting a lot of flack but I think there are two good patterns highlighted: 1) Avoid creating functions in React render and 2) Learn about and use defaultProps.<p>1: Example of arrow functions + Class property initializer: <a href="http:&#x2F;&#x2F;babeljs.io&#x2F;blog&#x2F;2015&#x2F;06&#x2F;07&#x2F;react-on-es6-plus&#x2F;" rel="nofollow">http:&#x2F;&#x2F;babeljs.io&#x2F;blog&#x2F;2015&#x2F;06&#x2F;07&#x2F;react-on-es6-plus&#x2F;</a> 2: Default Props: <a href="https:&#x2F;&#x2F;facebook.github.io&#x2F;react&#x2F;docs&#x2F;reusable-components.html#default-prop-values" rel="nofollow">https:&#x2F;&#x2F;facebook.github.io&#x2F;react&#x2F;docs&#x2F;reusable-components.ht...</a>
andrewingramover 9 years ago
Is it really an anti-pattern when you&#x27;re just writing code that depends on equality without understanding how equality works?<p>There&#x27;s probably a lesson to be learned in here, but if I wrote an article every time I wrote bad code, I wouldn&#x27;t have time to write any bad code ;)<p>Edit: This particular issue is pretty common, and it&#x27;s good to draw attention to it, but let&#x27;s just call it what it is - a common bug.
评论 #10994792 未加载
评论 #10996645 未加载
评论 #10995743 未加载
fiatjafover 9 years ago
This is not an anti-pattern, it&#x27;s a mistake you made.
danmaz74over 9 years ago
We just use Immutable.js in our Redux stores. Then, using PureRenderMixin is trivial. You just need to be careful not to use the vanilla reducer composer from Redux.
评论 #10994796 未加载
评论 #10995075 未加载
评论 #10994797 未加载
leomelinover 9 years ago
How do you avoid the e =&gt; fn() here in stateless functional component? This is very common case and I can&#x27;t see other workaround than to use redux mapStateToProps, or without redux maybe mapProps from recompose. Or to use something stupid like writing the item.id into data-attr-id on the li -element and digging out the id -value in the event handler...<p><pre><code> const List = props =&gt; &lt;ul&gt; props.items.map(item =&gt; &lt;li onClick={e =&gt; props.itemClicked(item.id)}&gt;{item.name}&lt;&#x2F;li&gt; ) &lt;&#x2F;ul&gt;</code></pre>
johnhenryover 9 years ago
I think that some of the issues that you&#x27;re running into might be mitigated by taking advantage of the &quot;key&quot; attribute on repeated elements. There&#x27;s more here: <a href="https:&#x2F;&#x2F;facebook.github.io&#x2F;react&#x2F;docs&#x2F;multiple-components.html#dynamic-children" rel="nofollow">https:&#x2F;&#x2F;facebook.github.io&#x2F;react&#x2F;docs&#x2F;multiple-components.ht...</a>
simplifyover 9 years ago
Is it wrong for me to think that having a thousand Cell elements on the page is the true anti-pattern? The user can really only see maybe 10-20 at one time. The cost of re-rendering on 15 rows should be trivial enough not to worry about creating a callback function in render.<p>Edit: To elaborate, you can solve this by using pagination (a common solution), or, if you want the user to be able to scroll and not click, occlusion culling. Someone out there must have written an occlusion culling library for React by now, surely.
评论 #10999140 未加载
_alexander_over 9 years ago
Where is anti-pattern?