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.
js is where you would usually mount/render your main react component onto your “root” element(which you mark in your html). “App” is what people tend to call the file which contains the main logic of your file, or in React case, the main component, the one that represents your entire application/web-site.
The major take here is that Next JS allows your developers to choose any of these pre-rendering options for every page. You can also combine its client-side and server-side capabilities for particular use cases. On the other hand, the Create React App doesn't have any provision for SSR by default.
Create React App (CRA) is a tool to create single-page React applications that is officially supported by the React team. The script generates the required files and folders to start the React application and run it on the browser.
ReactJS uses Node. js, a JavaScript runtime, to build your JavaScript code. Basically, the ReactJS is a JavaScript framework which needs the help of node js and npm(Package manager) to tell the native side(Android/iOS) that these are packages I need to run my app. And it adds all the dependencies needed to run the app.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With