[英]Is it possible to put a component inside a component?
我制作了一個我想重用的Form
組件,我還制作了一個我想重用的Submit
組件。 是否可以將Submit
組件放在Form
組件中?
export const Form = () => {
return (
<form>
</form>
)
}
提交.js
export const Submit = () => {
return (
<>
<input type="submit">
</>
)
}
測驗.js
export const Quiz = () => {
return (
<Form>
<Submit>
</Form>
)
}
您可以使用children
道具來實現這一點。
基本上,在您的 Form.js 中,使用props.children
export const Form = (props) => {
return (
<form>
{props.children}
</form>
)
}
然后在您的Quiz.js
中,像這樣使用它。
export const Quiz = () => {
return (
<Form>
<Submit />
</Form>
)
}
<Submit />
組件現在將在您的Form
組件中呈現
是的,您需要在顯示 {props.children} 上添加參數“props”以形成組件和 output:
const Form = (props) => {
return (
<form>
{props.children}
</form>
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.