繁体   English   中英

React Custom 组件未按预期呈现

[英]React Custom component is not getting rendered as expected

我期待文本:SMALL Medium,Big 在屏幕上呈现,但它没有被呈现


 function Box(prop){
        const ele = <div className={prop.cn}>

                    </div>

        return ele
      }

      const ele = <div className="container">
                       <Box cn='small'>SMALL</Box>
                       <Box cn='medium'>Medium</Box>
                       <Box cn='medium'>BIG</Box>
                  </div>

      ReactDOM.render(ele, document.getElementById('root'));  

Babel 正在将此 JSX 编译为下图,我不知道为什么儿童数组没有填充到 BOX function 中。 请帮助

在此处输入图像描述

利用:

function Box(prop){
    const ele = <div className={prop.cn}>
                     {prop.children} 
                </div>

    return ele
  }

您应该使用children道具为其中的 div 元素提供 Box 元素内容:

function Box({cn, children}){
  const ele = <div className={cn}>
                 {children} 
              </div>
  return ele
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM