亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React 自定義組件未渲染

React 自定義組件未渲染

慕神8447489 2023-10-14 16:42:17
*自定義組件未在 UI 上渲染,另請注意,我有 id: root 的元素function CComponent(prop) {  const element = (    <div className="container">      <div className={prop.classname}>{prop.content}</div>    </div>  );  return element;}const helloElement = (  <CComponent>{{ classname: "xyz", content: "helloWorld" }}</CComponent>);console.log(helloElement);ReactDOM.render(helloElement, document.getElementById("root"));
查看完整描述

4 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

您需要將 props 傳遞給組件,如下所示:

const helloElement = <CComponent classname='xyz' content='helloWorld' />


查看完整回答
反對 回復 2023-10-14
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您將作為孩子傳遞您的值,要將值作為道具傳遞,您可以這樣做:
<CComponent classname='xyz' content='helloWorld' />

查看完整回答
反對 回復 2023-10-14
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

根據反應文檔:


React.createElement(

   type,

   [props],

   [...children]

)

<CComponent>{{ classname: "xyz", content: "helloWorld" }}</CComponent>這是由 babel 編譯為:


var helloElement = React.createElement(CComponent, null, {

  classname: 'xyz',

  content: 'helloWorld'

})

但<CComponent classname='xyz' content='helloWorld' />


被編譯為


var helloElement= React.createElement(CComponent, {

  classname: "xyz",

  content: "helloWorld"

})  

因此在 UI 上呈現


查看完整回答
反對 回復 2023-10-14
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

使用 babel 7 和 React >= 16 是行不通的。CComponent 獲取帶有 {classname,content} 對象的 Children 屬性。

您可以嘗試稍微更改一下語法,它將完美呈現

<CComponent {...{ className: "xyz", content: "helloWorld" }} />


查看完整回答
反對 回復 2023-10-14
  • 4 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號