this.props.children({extra: 'things'}) 不起作用



我想像这样将一些状态传递给组件的子级(示例如下所示(

const WithExtraThings = ({ children }) => children({extra: 'things'})

上面的模式不起作用,所以我正在这样做

const WithExtraThings = ({ children }) =>
{React.Children.map(children, child => React.cloneElement(child, { extra: 'things' })}

你能确认第一个例子已经过时了吗?

您的第一个示例只要在单个函数中children即可工作。请参阅 reactjs 文档中的子级函数。

它根本没有过时,情况恰恰相反。这是本周取代HOC的新口味。

最新更新