How to consider defining a functional component inside a class component and outside the class in React?
Considering the following three places of defining a functional component 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 component 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;
javascript reactjs react-component
add a comment |
Considering the following three places of defining a functional component 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 component 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;
javascript reactjs react-component
1
Why are you not using the functional component like a regular component? Consider using jsx, not a function call.
– evolutionxbox
Nov 21 '18 at 8:35
@evolutionxbox Updated with JSX components but I could not make theFuncComponent2
work inside render().
– Abrar
Nov 21 '18 at 8:50
Also there’s no need to wrap string props in curly braces.
– evolutionxbox
Nov 21 '18 at 10:44
add a comment |
Considering the following three places of defining a functional component 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 component 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;
javascript reactjs react-component
Considering the following three places of defining a functional component 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 component 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;
javascript reactjs react-component
javascript reactjs react-component
edited Nov 21 '18 at 8:49
asked Nov 21 '18 at 8:33
Abrar
7061022
7061022
1
Why are you not using the functional component like a regular component? Consider using jsx, not a function call.
– evolutionxbox
Nov 21 '18 at 8:35
@evolutionxbox Updated with JSX components but I could not make theFuncComponent2
work inside render().
– Abrar
Nov 21 '18 at 8:50
Also there’s no need to wrap string props in curly braces.
– evolutionxbox
Nov 21 '18 at 10:44
add a comment |
1
Why are you not using the functional component like a regular component? Consider using jsx, not a function call.
– evolutionxbox
Nov 21 '18 at 8:35
@evolutionxbox Updated with JSX components but I could not make theFuncComponent2
work inside render().
– Abrar
Nov 21 '18 at 8:50
Also there’s no need to wrap string props in curly braces.
– evolutionxbox
Nov 21 '18 at 10:44
1
1
Why are you not using the functional component like a regular component? Consider using jsx, not a function call.
– evolutionxbox
Nov 21 '18 at 8:35
Why are you not using the functional component like a regular component? Consider using jsx, not a function call.
– evolutionxbox
Nov 21 '18 at 8:35
@evolutionxbox Updated with JSX components but I could not make the
FuncComponent2
work inside render().– Abrar
Nov 21 '18 at 8:50
@evolutionxbox Updated with JSX components but I could not make the
FuncComponent2
work inside render().– Abrar
Nov 21 '18 at 8:50
Also there’s no need to wrap string props in curly braces.
– evolutionxbox
Nov 21 '18 at 10:44
Also there’s no need to wrap string props in curly braces.
– evolutionxbox
Nov 21 '18 at 10:44
add a comment |
1 Answer
1
active
oldest
votes
You must avoid using functional component inside of render
since they will be recreated on every render.
As far as using functions that return JSX inside Class component
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 component outside of React component
is most advantageous when the same component can be used at multiple places and hence it makes sense to pass props to it and render it.
1
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407996%2fhow-to-consider-defining-a-functional-component-inside-a-class-component-and-out%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You must avoid using functional component inside of render
since they will be recreated on every render.
As far as using functions that return JSX inside Class component
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 component outside of React component
is most advantageous when the same component can be used at multiple places and hence it makes sense to pass props to it and render it.
1
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
add a comment |
You must avoid using functional component inside of render
since they will be recreated on every render.
As far as using functions that return JSX inside Class component
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 component outside of React component
is most advantageous when the same component can be used at multiple places and hence it makes sense to pass props to it and render it.
1
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
add a comment |
You must avoid using functional component inside of render
since they will be recreated on every render.
As far as using functions that return JSX inside Class component
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 component outside of React component
is most advantageous when the same component can be used at multiple places and hence it makes sense to pass props to it and render it.
You must avoid using functional component inside of render
since they will be recreated on every render.
As far as using functions that return JSX inside Class component
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 component outside of React component
is most advantageous when the same component can be used at multiple places and hence it makes sense to pass props to it and render it.
answered Nov 21 '18 at 8:46
Shubham Khatri
78.5k1492127
78.5k1492127
1
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
add a comment |
1
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
1
1
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
Got it. So, outside class is the best choice unless I would want the functional component to not be reused rather particularly used for that component only.
– Abrar
Nov 23 '18 at 18:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407996%2fhow-to-consider-defining-a-functional-component-inside-a-class-component-and-out%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why are you not using the functional component like a regular component? Consider using jsx, not a function call.
– evolutionxbox
Nov 21 '18 at 8:35
@evolutionxbox Updated with JSX components but I could not make the
FuncComponent2
work inside render().– Abrar
Nov 21 '18 at 8:50
Also there’s no need to wrap string props in curly braces.
– evolutionxbox
Nov 21 '18 at 10:44