admin管理员组文章数量:1390555
Considering the following three places of defining a functional ponent in React -
- Inside a class (outside the render method)
- Inside a class (inside the render method)
- Outside the class
In the sample code below, funcComponent1
, funcComponent2
and funcComponent3
are defined in the three different locations. How do I consider when to define a functional ponent in any of these 3 places?
import React, { Component } from 'react';
const FuncComponent1 = (props) => {
return (
<p>{props.name}</p>
)
}
class TestComponent extends Component {
constructor(props){
super(props);
this.state = {
name: "JavaScript"
}
}
FuncComponent2 = (text) => {
return (
<p>{text}, {this.state.name}</p>
)
}
render(){
const FuncComponent3 = (props) => {
return (
<p>{props.text}, {this.state.name}</p>
)
}
return (
<div>
<FuncComponent1 name={'Abrar'} text={'Hello World'}/>
<FuncComponent3 text={"HEllo World"}/>
</div>
)
}
}
export default TestComponent;
Considering the following three places of defining a functional ponent in React -
- Inside a class (outside the render method)
- Inside a class (inside the render method)
- Outside the class
In the sample code below, funcComponent1
, funcComponent2
and funcComponent3
are defined in the three different locations. How do I consider when to define a functional ponent in any of these 3 places?
import React, { Component } from 'react';
const FuncComponent1 = (props) => {
return (
<p>{props.name}</p>
)
}
class TestComponent extends Component {
constructor(props){
super(props);
this.state = {
name: "JavaScript"
}
}
FuncComponent2 = (text) => {
return (
<p>{text}, {this.state.name}</p>
)
}
render(){
const FuncComponent3 = (props) => {
return (
<p>{props.text}, {this.state.name}</p>
)
}
return (
<div>
<FuncComponent1 name={'Abrar'} text={'Hello World'}/>
<FuncComponent3 text={"HEllo World"}/>
</div>
)
}
}
export default TestComponent;
Share
Improve this question
edited Nov 21, 2018 at 8:49
Abrar
asked Nov 21, 2018 at 8:33
AbrarAbrar
7,2329 gold badges31 silver badges45 bronze badges
3
- 1 Why are you not using the functional ponent like a regular ponent? Consider using jsx, not a function call. – evolutionxbox Commented Nov 21, 2018 at 8:35
-
@evolutionxbox Updated with JSX ponents but I could not make the
FuncComponent2
work inside render(). – Abrar Commented Nov 21, 2018 at 8:50 - Also there’s no need to wrap string props in curly braces. – evolutionxbox Commented Nov 21, 2018 at 10:44
1 Answer
Reset to default 8You must avoid using functional ponent inside of render
since they will be recreated on every render.
As far as using functions that return JSX inside Class ponent
but outside render` is considered, you can do that when you want to make use of the state or props of the class in order to render JSX content but that which is very specific to the particular class
A functional ponent outside of React ponent
is most advantageous when the same ponent can be used at multiple places and hence it makes sense to pass props to it and render it.
本文标签:
版权声明:本文标题:javascript - How to consider defining a functional component inside a class component and outside the class in React? - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744591716a2614531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论