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.
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
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'));
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.
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.
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.
Related
I have been building a project on react using vscode and when i try to start it in the browser, I keep getting the error that it cant find the file index.js, even though it is in the right folder. (Yes I have imported react and not React). I have literally paused my project for a while day because of this and its weird because i was building a smaller project earlier on the same mahine, in the same way and it worked perfectly.
But I have no idea whats going on here, I have attached the code.
I tried moving the file around, checking my lowercase, deleteing the cache memory and restarting it, deleting the node modules folder and using npm insall, etc all that stuff already on stackOverflow but nothing works.
I am just starting out with react so I'm not sure what the problem is and I've tried googling as much as I could
Navbar.js:
import React from 'react'
import logo from './images/airbnb 1.png'
export default function Navbar(){
return (
<img src={logo} width= "40px"></img>
)
}
App.js:
import React from 'react'
export default function App(){
return(
<h1>App component</h1>
)
}
index.js:
import React from 'react'
import ReactDOM from 'react-DOM'
import App from './App'
import './index.css'
ReactDOM.render(<App />, document.getElementById("root"))
import ReactDOM from 'react-dom'
you are trying to import from react-DOM that does not exist
Hello I used flowbite along with tailwindcss and react js But the components are not working in the react
(You can click the links to see the directory Images)
I imported the flowbite in the index.js
Created a components called Drp stands for dropdown and add a dropdown from the flowbite components section
This Drp component you can see in the image
Then this is my tailwind.config.js :
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}", "./node_modules/flowbite/**/*.js"],
theme: {
extend: {},
},
plugins: [
require('flowbite/plugin')
],
}
but this is not working ,How to fix this???
Follow these points in "index.js" file:
Replace:
import ReactDOM from 'react-dom/client'; => import ReactDOM from 'react-dom';
User render method like:
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
I am trying Reactjs for the first time, but when want to render something, it only renders it when I restart my project. If I hit save, nothing happens. Have someone met with this problem yet?
import React from 'react';
function App(){
return (
<div>
<h1>Hello React</h1>
<button>Button</button>
</div>
);
}
export default App;
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<App />, document.getElementById('root'));
If you want the app to render the latest changes you made to the source files, you have to implement hot-reloading.
Check this site for more info : https://gaearon.github.io/react-hot-loader/getstarted/
It will scan changes you do to the source files and automatically apply the changes to the running app. It will also make sure that the state of the app is not lost during this change.
You completed the following configuration:
npm run eject run this command inside project root directory.
search cssRegex and add the following lines under use: getStyleLoaders({
modules:true,
localIdentName:'[name]__[local]__[hash:base64:5]
Now I just test the following code:
import React from 'react';
import logo from './logo.svg';
import classes from './App.css';
function App() {
return (
<div className={classes.App}>
<p>hello</p>
</div>
);
}
export default App;
As far i did, <div className={classes.App}> this should be converted as like this: <div class="APP_local_ANYTHING">. But no class is being generated. Whats wrong is going on?
if that's all the code you've got, app is not bootstrapped. In order to do so, you would have to add the folowing piece of code:
import * as ReactDOM from 'react-dom';
import App from 'app'; // file with you App component
ReactDOM.render(<App />, document.getElementById('root')) // or any other selector in HTML you want to have your app attached to.
Simple Codepen example
I carefully searched the same problem that I faced but didn't find anything helpful.
webpack 1.13.1. Mac OS X El Capitan last version.
What I am trying to accomplish
I want to create a webpack build to develop multiple page apps.
I want to see the structure like this:
source
/source/page1/page1.css
/source/page1/page1.js
/source/page1/page1.html
public
/public/page1/page1.css
/public/page1/page1.js
/public/page1/page1.html
Current build
This is what I've done.
https://gist.github.com/lavezzi1/9cc0e58cd7d2b8f2470e703022a41db7
I tested it with vue.js and it works perfect. But now I want to get it works with react.js as well.
In dev move everything works just fine but when I ran task for prod build I got this error:
ERROR in ./source/pages/index.js
Module not found: Error: a dependency to an entry point is not allowed
What I did. This is my page folder:
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<App />,
document.getElementById('root')
);
App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
So what am I doing wrong? I am newbie in react.js, maybe I don't understand something.
In vue.js I just create files:
index.html
index.css
index.vue
index.js
And everything works as expected.
I really really hope you guys help me.
why don't you just indicate the js file as entry point and import the css in that entry file. For multiple js entry file you can use the code splitting feature of webpack.
https://webpack.github.io/docs/code-splitting.html
and in your webpack.config.js file you can define entry file like the following.
entry: {
lite: "./lite.js", pro: "./pro.js",
},
output: { filename: "dist/[name].js" },