我们可以在类组件中使用功能组件,是否可以反过来做?例如
class MyClassComponent extends React.Component {
render() {
return (
<p>This is a class component with display {this.props.display}</p>
)
}
}
const MyFunctionComponent = (props) => {
return <MyClassComponent display=props.shouldDisplay>This is a function component</MyClassComponent >
}
答案 0 :(得分:3)
React使您透明使用的组件是函数还是类,因此您可以随意组合它们。
特别是在您的代码中,您可能需要重新考虑两个问题:
<MyClassComponent display={props.shouldDisplay}>
MyClassComponent
在this.props.children
中访问这些文本:const ChildComponent = (props) => {
return (
<div>
{this.props.children}
</div>
);
}
const ParentComponent = (props) => {
return (
<ChildComponent>
Hello World
</ChildComponent>
);
}
const App = (props) => {
return <ParentComponent/>;
}