【发布时间】:2018-11-21 01:44:46
【问题描述】:
我有一堆组件需要相同的 prop,并且单独通过它们添加 JSX 很麻烦,我可以在父组件中创建一个 JSX 将作为 prop 传递给这些子组件吗?
这基本上是我想做的:
function App(props) {
/*not part of the actual code, but I want to
pass this h1 tag into the bottom components as a prop*/
<h1>{props.heading}</h1>
<Home heading="Home"/>
<AboutMe heading="About Me"/>
<Contact heading="Contact"/>
}
我知道上面的代码不是你应该做的,但是有什么方法可以实现这个功能吗?
【问题讨论】:
-
如
<Home heading={ () => <h1>{props.heading}</h1> }/>?
标签: javascript reactjs react-props