Home > Blockchain >  Using const with arrow function returning component vs just the component in render()
Using const with arrow function returning component vs just the component in render()

Time:12-16

Is there any difference whatsoever if rendering the main component in the index.jsx of a React app like this:

import { App } from './App';
const root = document.getElementById('my-app');

const Program = () => {
    return <App />;
};

render(<Program />, root);

vs

import { App } from './App';
const root = document.getElementById('my-app');

render(<App />, root);

And is there ever a case to use the first option?

CodePudding user response:

How about this:

import { App } from './App';
const root = document.getElementById('my-app');

const Program = () => {
    return <App />;
};

const LayerTwo = () => {
  return <Program />
};


render(<LayerTwo />, root);

This will also return the same thing, but yet another layer. Because of KIS (keep it simple) the first option you mention should be avoided and the second option is the best:

import { App } from './App';
const root = document.getElementById('my-app');

render(<App />, root);

CodePudding user response:

Hi @Sometip,

It is a good idea to keep your code as simple and concise as possible. In this case, the second example is better because it does not introduce any unnecessary abstractions or complexity.

// importing React and render from the react and react-dom libraries 

import React from 'react';
import { render } from 'react-dom';

// Import the App component from the App.jsx file
import { App } from './App';

// Get the element with the ID "my-app" from the DOM
const root = document.getElementById('my-app');

// Render the App component inside the root element
render(<App />, root);

However, I like that way:-

import React from 'react';
import { render } from 'react-dom';
import { App } from './App';

const root = document.getElementById('my-app');
render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  root
);
  • Related