1

There's a component named TestComponent below, I wanted to ask what's the difference between me using React.render to render the test component in the same page and me using the TestComponent in the App.js

Scenario 1

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class TestComponent extends Component {
componentDidMount(){
    console.log("Great");
}

render() {
    return (
        <p>
            {this.props.test}
        </p>
    );
 }}
ReactDOM.render(<TestComponent test="new" />);

Scenario 2

App.js

import React, { Component } from 'react';
import './index.css';
import TestComponent from "./components/TestComponent"

class Apps extends Component {
  render() {

    return (
        <div>

             <TestComponent test={this.props.test}/>

        </div>
    );

}}
ReactDOM.render(<App test="new" />);
1
  • 1
    the only difference is the wrapping div that you have in Apps. But you can return just TestComponent and the result is the same. Commented Jun 22, 2018 at 10:21

1 Answer 1

2

There is no difference. The only difference is that in scenario one, you are rendering a component and in scenario two you are rendering a component that itself returns, as something to be rendered, another component. So there really is no difference.

It's like saying

int i = 3;
return i * i;

and alternatively doing

int i = 1;
return (i + 2) * (i + 2);

So it is really a matter of composition. Scenario one renders a component that is not composed of any other and scenario two renders a component that is composed of other components.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.