admin管理员组文章数量:1026989
We can use function ponent inside a class ponent, is it possible to do the other way around? For example
class MyClassComponent extends React.Component {
render() {
return (
<p>This is a class ponent with display {this.props.display}</p>
)
}
}
const MyFunctionComponent = (props) => {
return <MyClassComponent display=props.shouldDisplay>This is a function ponent</MyClassComponent >
}
We can use function ponent inside a class ponent, is it possible to do the other way around? For example
class MyClassComponent extends React.Component {
render() {
return (
<p>This is a class ponent with display {this.props.display}</p>
)
}
}
const MyFunctionComponent = (props) => {
return <MyClassComponent display=props.shouldDisplay>This is a function ponent</MyClassComponent >
}
Share
Improve this question
asked Apr 18, 2020 at 4:51
DrexDrex
3,87110 gold badges46 silver badges77 bronze badges
0
1 Answer
Reset to default 3React makes it transparent for you whether the ponents you use are functions or classes, so you can pose them as you like.
Specifically in your code there are two issues that you might want to reconsider:
- When you define a prop, its value should be wrapped in curly brackets:
<MyClassComponent display={props.shouldDisplay}>
- Components can be either self-closing or have children props. In your case, you've added text inside the opening tag and the closing tag, which you can access in
MyClassComponent
viathis.props.children
:
const ChildComponent = (props) => {
return (
<div>
{this.props.children}
</div>
);
}
const ParentComponent = (props) => {
return (
<ChildComponent>
Hello World
</ChildComponent>
);
}
const App = (props) => {
return <ParentComponent/>;
}
We can use function ponent inside a class ponent, is it possible to do the other way around? For example
class MyClassComponent extends React.Component {
render() {
return (
<p>This is a class ponent with display {this.props.display}</p>
)
}
}
const MyFunctionComponent = (props) => {
return <MyClassComponent display=props.shouldDisplay>This is a function ponent</MyClassComponent >
}
We can use function ponent inside a class ponent, is it possible to do the other way around? For example
class MyClassComponent extends React.Component {
render() {
return (
<p>This is a class ponent with display {this.props.display}</p>
)
}
}
const MyFunctionComponent = (props) => {
return <MyClassComponent display=props.shouldDisplay>This is a function ponent</MyClassComponent >
}
Share
Improve this question
asked Apr 18, 2020 at 4:51
DrexDrex
3,87110 gold badges46 silver badges77 bronze badges
0
1 Answer
Reset to default 3React makes it transparent for you whether the ponents you use are functions or classes, so you can pose them as you like.
Specifically in your code there are two issues that you might want to reconsider:
- When you define a prop, its value should be wrapped in curly brackets:
<MyClassComponent display={props.shouldDisplay}>
- Components can be either self-closing or have children props. In your case, you've added text inside the opening tag and the closing tag, which you can access in
MyClassComponent
viathis.props.children
:
const ChildComponent = (props) => {
return (
<div>
{this.props.children}
</div>
);
}
const ParentComponent = (props) => {
return (
<ChildComponent>
Hello World
</ChildComponent>
);
}
const App = (props) => {
return <ParentComponent/>;
}
本文标签: javascriptHow could I use a class component inside function componentStack Overflow
版权声明:本文标题:javascript - How could I use a class component inside function component - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656237a2161614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论