如果组件中没有任何内部状态要跟踪,则react中的功能组件最好使用。
但是我想要的是访问无状态组件的children
,而不必扩展React.Component
,我可以使用它来使用props.children
。这可能吗 ?
如果是这样,该怎么办?
最佳答案
我们可以在功能组件中使用props.children。无需使用基于类的组件。
const FunctionalComponent = props => {
return (
<div>
<div>I am inside functional component.</div>
{props.children}
</div>
);
};
调用功能组件时,可以执行以下操作-
const NewComponent = props => {
return (
<FunctionalComponent>
<div>this is from new component.</div>
</FunctionalComponent>
);
};
希望这能回答你的问题。