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.

Use a Render Prop

73 pointsby freeman478over 7 years ago

8 comments

wereHamsterover 7 years ago
People seem to forget that components (stateless components, stateless functional components or whatever you want to call them) are just functions. One function calls another function, that's function composition. In plain JavaScript we do it all the time. But when React is involved people are suddenly all like: no you can't call that function directly, you have to name it 'Component' with a capital C and use like '<Component />' and to compose two Components you have to use a HOC. It's like they all forget how to JavaScript…
评论 #15666546 未加载
评论 #15665065 未加载
pspeter3over 7 years ago
As a note, it is not safe for a component using a render prop to implement shouldComponentUpdate. The problem is that the render prop may close over data that the component is unaware of. The Asana core components used to the use the render prop pattern until we discovered that issue and also realized it made tests harder to write. Now we use React.cloneElement instead.
评论 #15664118 未加载
评论 #15664115 未加载
catpoliceover 7 years ago
I started today by setting out to write a response of the form &quot;Certainly you couldn&#x27;t rewrite MY HOC library to use render props, look at how it [etc]&quot; and ended today having rewritten my HOC library to use render props. In the process, I was able to dramatically simplify the API and remove about a third of the overall code.<p>So, uh, thanks.
couchandover 7 years ago
This technique is very useful, but passing the callback as a prop is an ugly way to do it. Much cleaner to pass the callback as children [0].<p>Then, the final example looks like:<p><pre><code> &lt;Mouse&gt; {({ x, y }) =&gt; ( &lt;h1&gt;The mouse position is ({x}, {y})&lt;&#x2F;h1&gt; )} &lt;&#x2F;Mouse&gt; </code></pre> [0]: <a href="https:&#x2F;&#x2F;discuss.reactjs.org&#x2F;t&#x2F;children-as-a-function-render-callbacks&#x2F;626" rel="nofollow">https:&#x2F;&#x2F;discuss.reactjs.org&#x2F;t&#x2F;children-as-a-function-render-...</a>
评论 #15664622 未加载
arenaninjaover 7 years ago
My biggest issue with HOCs is the hard time you have when things are nested to high hell. A component like withThis(withThat(enrich(withState(withPropsOnChange(withPropsOnChange(withPropsOnChange(withPropsOnChange(....... when this component gives you an issue you begin to wonder what HOCs are really doing for you.<p>Having a render prop is slightly better but even this escape hatch isn&#x27;t foolproof and you&#x27;ll still end up needing things like onClickOutside.
评论 #15665607 未加载
评论 #15666584 未加载
reichardtover 7 years ago
Neat! Didn&#x27;t know about this concept. Any more examples of this being used somewhere?
评论 #15661252 未加载
评论 #15663864 未加载
评论 #15661942 未加载
hguhghuffover 7 years ago
What would the code look like to use this approach for Redux.<p>Could Redux boilerplate be reduced?
评论 #15663632 未加载
tootieover 7 years ago
Even using the term &quot;higher order&quot; that seems like a very OOP solution, while render props is very functional and thus a better idiom for JavaScript.
评论 #15663117 未加载
评论 #15666881 未加载