61

I started learning React and now I'm trying to understand what is the purpose of the index.js and App.js which are created by by running create-react-app.

Why can't we just use, for example. App.js?

I've read that App.js usually used as a main entry point to the application, but auto-generated code of index.js seems like a part of main entry point:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

I saw a similar questions for react native but I want to know about this in react in general.

Artem Malchenko
  • 2,320
  • 1
  • 18
  • 39

6 Answers6

68

index.js is the traditional and actual entry point for all node apps. Here in React it just has code of what to render and where to render.

App.js on the other hand has the root component of the react app because every view and component are handled with hierarchy in React, where <App /> is the top most component in the hierarchy. This gives you the feel that you maintain hierarchy in your code starting from App.js.

Other than that, you can have the App logic in the index.js file itself. But it's something related to conventions followed by the community using the library or framework. It always feels good to go along the community.

Jasperan
  • 2,154
  • 1
  • 16
  • 40
Hariharan L
  • 1,291
  • 1
  • 11
  • 17
  • 9
    Yep, `App` is really just another component! `index.js` is the canonical file – information_interchange Jan 18 '19 at 07:16
  • 3
    index.js is intended to be still "DOM world" and linking to the "React world", whereas App.js is a 100% "React world" component. You might have other "DOM world" code in index.js (which is uncommon though) but not in App.js. – kca Jan 15 '22 at 09:11
12

Great question. The answer is that App.js is not necessary and I personally delete it and just use Index.js as the root.

People "say" it makes it look nicer / easier but it is just adding an extra step that is not necessary. I hope that they will eventually delete App.js out of npx create-react-app and just use index.js.

Edit: I'm not going to alter my original comment, however, I gave up deleting App.js. I now just funnel everything through App.js and use Index.js. The nice thing about index.js is you can do all your imports there and funnel them through App.js

Evan Erickson
  • 471
  • 5
  • 12
  • 1
    If you can present some code to show what you mean that would help. How would index.js look like without App component? – VivekDev Aug 02 '21 at 10:20
  • @VivekDev Literally the same. Just take out the APP component and then put your App stuff inside of it. – Evan Erickson Aug 02 '21 at 21:48
  • 3
    App.js is not "necessary", but it is not at all "necessary" to organize the app in components in any way, you could just put everything in one large file. The two files are meant to keep a clean separation between plain JS and React. (But no problem to organize your code differently if you don't need/want it). – kca Jan 15 '22 at 09:17
5

Yes, App.js is not necessary. You can have just the index.js as follows.

// Import React and ReactDOM Libraries.
import React from 'react';
import ReactDOM from 'react-dom';
import CommmentDetail from './CommentDetail';

function getLabelText() {
  return 'Enter Name: ';
}

// Create React Components 
const App = () => {
  const buttonText = {text: 'Submit'};
  const buttonStyle = {backgroundColor: 'blue', color: 'white'}; 
  return (
    <div>
      <label className="label" htmlFor="name">{getLabelText()}</label>  
      <input id="name" type="text" />
      <button style={buttonStyle} >{buttonText.text}</button>

      // You can have other components here as follows.
      // CommmentDetail is someOther component defined in another file.
      // See the import statement for the same, 4th line from top

      <CommmentDetail author='Nivesh' timeAgo='3 months ago at 4.45 PM' commentText='Good Point' } />
    </div>
  )
}

// Take the react component and show it on the screen
// ReactDOM.render(<App />, document.getElementById('root'));
// You can use the following as well.
ReactDOM.render(<App />, document.querySelector('#root'));
VivekDev
  • 20,868
  • 27
  • 132
  • 202
1
import React from "react";
import ReactDOM from "react-dom";

function App() {
  return (
    <div className="App">
      <h1>Hello</h1>
      <h2>How are you!</h2>
    </div>
  );
}

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById("root")
);

This is a sample where we can directly implement without using App.js.

0

App.js is the "top component" that contains the logic of your Application whereas index.js is the "entry point" for your server and contains the logic of the server.

This can also help to have multiple Applications if needed to switch from one to the other without changing anything in the server.

-2

create-react-app use a plugin for webpack that name is html-webpack-plugin and this plugin use index.js for entrypoint like below :

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
 entry: 'index.js',
 output: {
path: __dirname + '/dist',
filename: 'index_bundle.js'
},
 plugins: [
new HtmlWebpackPlugin()
 ]
}

this plugin use for generating html file.

MBehtemam
  • 7,865
  • 15
  • 66
  • 108
  • I guess it's not very precise, html-webpack-plugin is using `entry` from webpack configuration. – Evgeny Timoshenko May 23 '18 at 16:46
  • @EvgenyTimoshenko according to [this quesiton](https://stackoverflow.com/questions/42438171/create-react-app-index-html-and-index-js-connection) Dan Abramov the creator of Redux saied this. – MBehtemam May 23 '18 at 16:51
  • @EvgenyTimoshenko and also [this configuration](https://github.com/facebook/create-react-app/blob/21fe19ab0fbae8ca403572beb55b4d11e45a75cf/packages/react-scripts/config/webpack.config.prod.js#L68-L70) – MBehtemam May 23 '18 at 16:52
  • correct. html-webpack-plugin is using whatever is specified in webpack config. my first comment is not correct, html-webpack-plugin is using neither `entry` nor `index.js`, it's using `output` – Evgeny Timoshenko May 23 '18 at 18:11