admin管理员组

文章数量:1397175

I want to start learning React but how do I install it and then have the possibility to import. I tried: npm install react react-dom It download node_modules and package-lock.json but still it doesn't work. Here is the code i tried index.js

import React from "react"
import ReactDom from "react-dom"

ReactDOM.render(<h1>Hello World</h1>, document.getElementByID('root'));

HTML Code index.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="./js/underscore.js"></script>
      <script src="./js/backbone.js"></script>
      <script src="index.js"></script>
      <title>Document</title>
   </head>
   <body>
      <div id="root"></div>
   </body>
</html>

I want to start learning React but how do I install it and then have the possibility to import. I tried: npm install react react-dom It download node_modules and package-lock.json but still it doesn't work. Here is the code i tried index.js

import React from "react"
import ReactDom from "react-dom"

ReactDOM.render(<h1>Hello World</h1>, document.getElementByID('root'));

HTML Code index.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="./js/underscore.js"></script>
      <script src="./js/backbone.js"></script>
      <script src="index.js"></script>
      <title>Document</title>
   </head>
   <body>
      <div id="root"></div>
   </body>
</html>
Share Improve this question edited Feb 7 at 9:52 Mark Rotteveel 110k229 gold badges156 silver badges224 bronze badges asked Mar 25, 2020 at 19:59 Rina5Rina5 371 gold badge1 silver badge5 bronze badges 4
  • 4 reactjs/docs/getting-started.html – Heretic Monkey Commented Mar 25, 2020 at 20:02
  • 2 Use create-react-app – DumbCoder7 Commented Mar 25, 2020 at 20:03
  • "It doesn't work" is not a problem statement we can help with. – Ian Kemp - SE killed by LLMs Commented Mar 25, 2020 at 20:28
  • ReactDom in your import statement and ReactDOM.render doesn't match please modify that along with document.getElementById – Ram Sankhavaram Commented Mar 25, 2020 at 20:36
Add a ment  | 

4 Answers 4

Reset to default 4

from react's documentation:

Create React App is a fortable environment for learning React, and is the best way to start building a new single-page application in React.

So I would suggest you start with create-react-app. Its a pre-made environment with everything set up so until you're ready you don't have to worry about it.

  1. first run: npx create-react-app my-app

This will create a new folder in your current directory and inside it is a ready made react application.

  1. Enter the directory by running cd my-app

  2. and then run this mand npm start to start your react app

If you don't know how to run mands, google "how to run console mands in windows/mac/linux".

You have a spelling error in your code

ReactDOM.render(<h1>Hello World</h1>, document.getElementByID('root'));

Should be

ReactDOM.render(<h1>Hello World</h1>, document.getElementById('root'));

Also change your ReactDom import to

import ReactDOM from "react-dom"

Fix Your Current Setup (Without Create React App)

If you want to keep using your current setup, you need Babel and Webpack to pile JSX. Here's how to fix your setup:

Run this mand in your project folder:

npm install react react-dom
npm install babel-loader @babel/core @babel/preset-react @babel/preset-env 
webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev

Your code has two small mistakes:

  1. ReactDom should be ReactDOM (uppercase "DOM").
  2. getElementByID should be getElementById (lowercase "d").

corrected index.js

import React from "react";
import ReactDOM from "react-dom";

ReactDOM.render(<h1>Hello World</h1>, document.getElementById('root'));

Now Set Up Babel and Webpack

Create a webpack.config.js file in your project root:

import path from 'path';
import { fileURLToPath } from 'url';
import HtmlWebpackPlugin from 'html-webpack-plugin';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
    }),
  ],
  devServer: {
    static: path.resolve(__dirname, 'dist'),
    port: 3000,
    open: true,
  },
};

Create Babel Configuration

Create a file called .babelrc:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

Fix Your HTML (index.html)

Your index.html should not manually load index.js. Instead, let Webpack handle it.

Move your index.html to a src folder (/src/index.html):

In package.json, add these scripts:

"scripts": {
  "start": "webpack serve --mode development",
  "build": "webpack --mode production"
}

Now start your React app with

npm start

if you are using Visual Studio in Windows, First Install Node Js in your Device, and then in Vs code run the Create-React-App and add your app name and you are good to go then also install the react-router-dom in your app

本文标签: javascriptHow to install React and ReactDomStack Overflow