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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744591717a4582645.html
评论列表(0条)