Home Web Front-end JS Tutorial Detailed steps to build a React development environment using create-react-app

Detailed steps to build a React development environment using create-react-app

May 29, 2018 am 11:52 AM
react develop

This time I will give you a detailed explanation of the steps to use create-react-app to build a React development environment. What are the precautions for using create-react-app to build a React development environment. The following is a practical case. Let’s take a look.

Commonly used scaffolding

  1. react-boilerplate

  2. react- redux-starter-kit

  3. create-react-app (most followed on git)

Use create- react-app quickly builds a React development environment

create-react-app comes from Facebook. With this command, we can quickly build a React development environment without configuration.

create-react-app The automatically created project is based on Webpack ES6.

Execute the following commandCreate project:

$ cnpm install -g create-react-app
$ create-react-app my-app
$ cd my-app/
$ npm start
Copy after login

tips:

If using npm is very slow, you can use Taobao’s customized cnpm (gzip compression Support) command line tool instead of the default npm:

$ npm install -g cnpm --registry=https://registry.npm.taobao.org
$ npm config set registry https://registry.npm.taobao.org
Copy after login

How to compile less

1. Expose the configuration file

npm run eject
Copy after login
Copy after login

2.Installless-loader and less

npm install less-loader less --save-dev
Copy after login

3. Modify webpack configuration

Modify webpack.config.dev.js and webpack. config-prod.js configuration file

test: /\.css$/ 改为 /\.(css|less)$/
Copy after login

Find use under this test, add loader

{
  loader: require.resolve('less-loader') // compiles Less to CSS
}
Copy after login

Okay, re-run the project less has been compiled

Project structure

src directory

The src directory is used to store our own code. You can create subdirectories under src, only src Files in the root directory will be compiled by webpack, so the files must be placed in the src root directory, otherwise they will not be recognized.

public directory

Only files in the public directory will be referenced by public/index.html

tip: index under public and src. The html file must exist and cannot be renamed

Common commands

1Start command

npm start
Copy after login

2Compile and package command. Compile the code in the production environment and place it in the build directory to correctly package the code, optimize, compress, and use hash to rename the file

npm run build
Copy after login
Copy after login

3. Test after the file is modified

npm test
Copy after login

4 .structure command. The configuration command of webpck is exposed. Originally in the scaffolding, the configuration item is invisible and needs to be modified before executing this command (this is a single command, once structured, it is irreversible.)

npm run eject
Copy after login
Copy after login

5.httpsRun

set HTTPS=true&&npm start
Copy after login

6. Online compilation

This is a big highlight of create-react-app. It allows your application to decode the code and compile the files that run in the online production environment. It is very small, and the file name also has a hash value, which is convenient for us to cache, and it also provides a server so that we can see similar effects to the online production environment locally, which is really super convenient.

Just one line of command:

npm run build
Copy after login
Copy after login

7.api development

Just add a configuration item to the package.json file.

"proxy": http://localhost:3001/,
Copy after login

8.ajax?
Instead of using fetch

npm install whatwg-fetch
Copy after login
import 'whatwg-fetch'
fetch('/power-mnjy-mobile/intf/h5/user/login?mobile=13000000001&pwd=a111111&ignoreCsrfToken=true')
  .then(res => res.json())
  .then((data) => {
   this.setState({ value:data });
  });
Copy after login

Debugging tool react developer tools (https://github.com/facebook/react-devtools)

Add the extension react developer tools in chrome, remember Restart the browser (restart, restart, restart, say important things three times), and the react option will appear in the chrome development tools.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use AngularJS to implement tab switching

How to use Koa2 to develop WeChat 2D Scan the QR code to pay

The above is the detailed content of Detailed steps to build a React development environment using create-react-app. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Which Linux distribution is best for Android development? Which Linux distribution is best for Android development? Mar 14, 2024 pm 12:30 PM

Android development is a busy and exciting job, and choosing a suitable Linux distribution for development is particularly important. Among the many Linux distributions, which one is most suitable for Android development? This article will explore this issue from several aspects and give specific code examples. First, let’s take a look at several currently popular Linux distributions: Ubuntu, Fedora, Debian, CentOS, etc. They all have their own advantages and characteristics.

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Understanding VSCode: What is this tool used for? Understanding VSCode: What is this tool used for? Mar 25, 2024 pm 03:06 PM

"Understanding VSCode: What is this tool used for?" 》As a programmer, whether you are a beginner or an experienced developer, you cannot do without the use of code editing tools. Among many editing tools, Visual Studio Code (VSCode for short) is very popular among developers as an open source, lightweight, and powerful code editor. So, what exactly is VSCode used for? This article will delve into the functions and uses of VSCode and provide specific code examples to help readers

See all articles