最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Render method in React carbon design component - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

passing 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
}} />
发布评论

评论列表(0)

  1. 暂无评论