admin管理员组文章数量:1305075
I'm working with Carbon design & React - following the example:
<HeaderContainer render={({
isSideNavExpanded,
onClickSideNavExpand
}) => {
console.log("Rendering", onClickSideNavExpand)
console.log("Rendering", isSideNavExpanded)
return (<> ...
<HeaderMenuButton aria-label={isSideNavExpanded ? 'Close menu' : 'Open menu'} onClick={onClickSideNavExpand} isActive={isSideNavExpanded} aria-expanded={isSideNavExpanded} />
my stuff
...</>)
}/>
I don't understand the render method here. What is the difference between this method and between using "children", something like:
<HeaderContainer>
<>
<HeaderMenuButton aria-label={isSideNavExpanded ? 'Close menu' : 'Open menu'} onClick={onClickSideNavExpand} isActive={isSideNavExpanded} aria-expanded={isSideNavExpanded} />
... my stuff ...
</>
</HeaderContainer>
Why implement it the first way instead of the second? I think it's much less readable...
I'm working with Carbon design & React - following the example:
<HeaderContainer render={({
isSideNavExpanded,
onClickSideNavExpand
}) => {
console.log("Rendering", onClickSideNavExpand)
console.log("Rendering", isSideNavExpanded)
return (<> ...
<HeaderMenuButton aria-label={isSideNavExpanded ? 'Close menu' : 'Open menu'} onClick={onClickSideNavExpand} isActive={isSideNavExpanded} aria-expanded={isSideNavExpanded} />
my stuff
...</>)
}/>
I don't understand the render method here. What is the difference between this method and between using "children", something like:
<HeaderContainer>
<>
<HeaderMenuButton aria-label={isSideNavExpanded ? 'Close menu' : 'Open menu'} onClick={onClickSideNavExpand} isActive={isSideNavExpanded} aria-expanded={isSideNavExpanded} />
... my stuff ...
</>
</HeaderContainer>
Why implement it the first way instead of the second? I think it's much less readable...
Share Improve this question asked Feb 3 at 21:49 mikebmikeb 11.3k8 gold badges69 silver badges131 bronze badges1 Answer
Reset to default 1passing a render function is called render prop
The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function.
A component with a render prop takes a function that returns a React element and calls it instead of implementing its own render logic.
if isSideNavExpanded
and onClickSideNavExpand
exist inside of HeaderContainer
, you can't pass them to HeaderMenuButton
as in your second example.
const Component = ({render}) => {
const localData = ... // this lies inside the children component
return render(localData) // this is how the parent access the data
}
<Component render={(dataFromComponent) => { // access data from Component
...do something with data
return view
}} />
本文标签: reactjsRender method in React carbon design componentStack Overflow
版权声明:本文标题:reactjs - Render method in React carbon design component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741795889a2397937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论