Table of Contents
方法1:只使用React,不使用JSX
方法2:通过browser.js转换JSX/ES 2015(非生产设置)
方法3:通过system.js/browser.js在浏览器中转找JSX/ES 2015(非生产设置)
Hello {name}!
方法4:使用在线编辑器创建React
方法5:在开发过程中使用Babel-cli和npm转换JSX/ES 2015
第一步:确定安装了node.js和npm,然后安装全局packages
第二步:创建目录和文件
第三步:通过npm安装依赖关系
第四步:配置Babel和npm
第五步: 更新index.html
第六步:更新app.js
第七步:运行Babel和Server
方法6:通过Webpack和Babel-core在开发过程中转换JSX/ES 2015
第四步:更新app.js,app.css,math.js和index.html
第五步:更新webpack.config.js
第六步:更新package.json
第七步:运行webpack和server
方法7:通过Babel-core和SystemJs/jspm.io在开发中转换JSX/ES 2015
步骤一:确保安装 node.js 和 npm ,然后安装全局packages
步骤二:创建文件夹和文件
步骤三:安装npm依赖关系
步骤四:初始化SystemJS/JSPM设置
步骤五: 更新app.js,app.css,math.js和index.html
步骤六: 使用jspm-cli安装开发包
步骤七: 更新package.json
步骤八:运行服务器
步骤九:捆绑模式
开始使用React
大漠
Home Web Front-end HTML Tutorial React几种基本配置方案_html/css_WEB-ITnose

React几种基本配置方案_html/css_WEB-ITnose

Jun 21, 2016 am 08:47 AM

对于没有使用过React的同学总觉得它复杂,但在现实中,使用React并不困难。就我个人而言,学习React应该基于创建项目特定类型的设置细节之上(比如Webpack、Redux、ES6、JSX、Babel等),而不是一下子就去忙于理解所有的设置项。

在这篇文章中列出了有关于React方面的七种设置。大部分的设置我都将会向大家展示,但总的来说,这并不困难。接下来的内容从简单到复杂,介绍React的设置。

方法1:只使用React,不使用JSX

如果在React项目中决定不使用JSX,又想渲染HTML DOM。那么在准备写React代码之前,在你的HTML页面需要引入一个 react.js 和 react-dom.js 文件。

react.js 文件是创建React节点和组件所需要的核心文件。当你打算在一个HTML中渲染一个组件(比如DOM)还需要 react-dom.js 文件。 react-dom.js 文件依赖于 react.js 文件,所以在引入 react-dom.js 文件之前要先引入 react.js 文件。比如下面的示例:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>React</title>    <script src="./src/js/react.js"></script>    <script src="./src/js/react-dom.js"></script></head><body></body></html>
Copy after login

在HTML页面中使用 react.js 和 react-dom.js 文件,就可以创建React节点或组件,然后渲染成DOM。接下来创建一个名叫 HelloMessage 的React组件,并且放到

的React节点中,最后渲染到
的HTML元素内。

<body>    <div id="app"></div>    <script>        var HelloMessage = React.createClass ({            displayName: "HelloMessage",            render: function render () {                return React.createElement("div", null, "Hello ", this.props.name);            }        });        ReactDOM.render(React.createElement(HelloMessage, {name: "John"}), document.getElementById("app"));    </script></body>
Copy after login

这样使用不需利用JSX或ES 2015。

方法2:通过browser.js转换JSX/ES 2015(非生产设置)

可以按前面的方式,在HTML页面中添加一个额外的脚本,允许使用JSX/ES2015。然后在客户端使用Babel来转换JSX并不是一个适于生产。在客户端运行时处理JSX/ES2015时负担很重,但对于非生产环境下在HTML中添加 browser.js 文件,可以在客户端中运行时转换JSX。

在HTML页面中使用JSX来实现前面示例中 HelloMessage 组件:

<body>    <div id="app"></div>    <script type="text/babel">        const HelloMessage = React.createClass({            render: function (){                return <div>Hello {this.props.name}</div>;            }        });        ReactDOM.render(<HelloMessage name="Jhon" />, document.getElementById("app"));    </script></body>
Copy after login

代码的转换发生了,那是因为我们引入了 browser.js 的Babel文件,并且给

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

See all articles