如何将 Prop 传给 child ?
const Tabs = ({ children, props }) => {
console.log(props) //where is activeTab??
return React.Children.map(children, child =>
React.cloneElement(child, { ...props })
)
}
const App = () => {
return (
<Tabs activeTab={1}>
<div>tabs children</div>
</Tabs>
);
};
期望 props 是一个对象,其中 activeTab 等于 1 但上面的代码给了我未定义?
最佳答案
这里不需要解构。像这样接受 props
作为参数
const Tabs = ( props ) => {
// ... props.children - is your children object
}
如果你想使用解构它会像这样
const Tabs = ( {children, activeTab } ) => {
// now children and activeTab is passed props
}
关于javascript - 传递 Prop 给 child 丢失 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49903561/