本文转自:http://www.runoob.com/react/react-install.htmlcss
React 能够直接下载使用,下载包中也提供了不少学习的实例。html
本教程使用了 React 的版本为 15.4.2,你能够在官网 http://facebook.github.io/react/ 下载最新版。node
你也能够直接使用 BootCDN 的 React CDN 库,地址以下:react
如下实例输出了 Hello, world!webpack
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Hello React!</title> <script src="https://cdn.bootcss.com/react/15.4.2/react.min.js"></script> <script src="https://cdn.bootcss.com/react/15.4.2/react-dom.min.js"></script> <script src="https://cdn.bootcss.com/babel-standalone/6.22.1/babel.min.js"></script> </head> <body> <div id="example"></div> <script type="text/babel"> ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('example') ); </script> </body> </html>
实例中咱们引入了三个库: react.min.js 、react-dom.min.js 和 babel.min.js:git
以上代码将一个 h1 标题,插入 id="example" 节点中。github
注意:web
若是咱们须要使用 JSX,则 <script> 标签的 type 属性须要设置为 text/babel。express
若是你的系统还不支持 Node.js 及 NPM 能够参考咱们的 Node.js 教程。npm
咱们建议在 React 中使用 CommonJS 模块系统,好比 browserify 或 webpack,本教程使用 webpack。
国内使用 npm 速度很慢,你能够使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org $ npm config set registry https://registry.npm.taobao.org
这样就能够使用 cnpm 命令来安装模块了:
$ cnpm install [name]
更多信息能够查阅:http://npm.taobao.org/。
create-react-app 是来自于 Facebook,经过该命令咱们无需配置就能快速构建 React 开发环境。
create-react-app 自动建立的项目是基于 Webpack + ES6 。
执行如下命令建立项目:
$ cnpm install -g create-react-app $ create-react-app my-app $ cd my-app/ $ npm start
在浏览器中打开 http://localhost:3000/ ,结果以下图所示:
项目的目录结构以下:
my-app/ README.md node_modules/ package.json .gitignore public/ favicon.ico index.html src/ App.css App.js App.test.js index.css index.js logo.svg
尝试修改 src/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>欢迎来到菜鸟教程</h2> </div> <p className="App-intro"> 你能够在 <code>src/App.js</code> 文件中修改。 </p> </div> ); } } export default App;
修改后,打开 http://localhost:3000/ (通常自动刷新),输出结果以下:
tianqixin
429***967@qq.com
参考地址
create-react-app 执行慢的解决方法:
在使用 create-react-app my-app 来建立一个新的React应用,在拉取各类资源时,每每会很是慢,一直卡在那:
fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch
能够看到资源仍是使用了 npmjs.org,解决方法是换成淘宝的资源:
$ npm config set registry https://registry.npm.taobao.org -- 配置后可经过下面方式来验证是否成功 $ npm config get registry -- 或 npm info express