我想制作一個組件,但我希望它也接受元素。例如 :
零件 :
const Example = () => {
return (
<div>
//Some Elements will come here.
</div>
)
}
另一個頁面:
const App = () => {
return (
<Example>
<div>
<h1>Hello all </h1>
<p>I want that elements acceptable on my custom component </p>
</div>
</Example>
)
}
但我只能發送道具,我不能在我的組件的標簽內寫任何東西。我怎樣才能做到?謝謝大家!
uj5u.com熱心網友回復:
React 定義了一個名為children. 這正是你所需要的。
像這樣嘗試
const Example = ({ children }) => {
return <div>{children}</div>;
};
const App = () => {
return (
<Example>
<div>
<h1>Hello all </h1>
<p>I want that elements acceptable on my custom component </p>
</div>
</Example>
);
};
uj5u.com熱心網友回復:
你可以使用props.children
const Example = props => {
return <div>{props.children}</div>;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/380017.html
標籤:javascript html 反应
上一篇:事件不會在第一次點擊父孩子時觸發
