|
| 1 | +--- |
| 2 | +id: getting-started-zh-CN |
| 3 | +title: 入门教程 |
| 4 | +layout: docs |
| 5 | +next: tutorial.html |
| 6 | +--- |
| 7 | + |
| 8 | +## JSFiddle |
| 9 | + |
| 10 | +开始 Hack React 的最简单的方法是用下面 JSFiddle 的Hello Worlds |
| 11 | + |
| 12 | + * **[React JSFiddle](http://jsfiddle.net/vjeux/kb3gN/)** |
| 13 | + * [React JSFiddle without JSX](http://jsfiddle.net/vjeux/VkebS/) |
| 14 | + |
| 15 | +## 入门教程包 (Starter Kit) |
| 16 | + |
| 17 | +开始先下载入门教程包 |
| 18 | + |
| 19 | +<div class="buttons-unit downloads"> |
| 20 | + <a href="/react/downloads/react-{{site.react_version}}.zip" class="button"> |
| 21 | + 下载入门教程 {{site.react_version}} |
| 22 | + </a> |
| 23 | +</div> |
| 24 | + |
| 25 | +在入门教程包的根目录,创建一个含有下面代码的 `helloworld.html` |
| 26 | + |
| 27 | +```html |
| 28 | +<!DOCTYPE html> |
| 29 | +<html> |
| 30 | + <head> |
| 31 | + <script src="build/react.js"></script> |
| 32 | + <script src="build/JSXTransformer.js"></script> |
| 33 | + </head> |
| 34 | + <body> |
| 35 | + <div id="example"></div> |
| 36 | + <script type="text/jsx"> |
| 37 | + /** @jsx React.DOM */ |
| 38 | + React.renderComponent( |
| 39 | + <h1>Hello, world!</h1>, |
| 40 | + document.getElementById('example') |
| 41 | + ); |
| 42 | + </script> |
| 43 | + </body> |
| 44 | +</html> |
| 45 | +``` |
| 46 | + |
| 47 | +在 JavaScript 代码里写着 XML 格式的代码称为 JSX;可以去 [JSX 语法](/react/docs/jsx-in-depth.html) 里学习更多 JSX 相关的知识。为了把 JSX 转成标准的 JavaScript,我们用 `<script type="text/jsx">` 标签包裹着含有 JSX 的代码,然后引入 `JSXTransformer.js` 库来实现在浏览器里的代码转换。 |
| 48 | + |
| 49 | +### 分离文件 |
| 50 | + |
| 51 | +你的 React JSX 代码文件可以写在另外的文件里。新建下面的 `src/helloworld.js`。 |
| 52 | + |
| 53 | +```javascript |
| 54 | +/** @jsx React.DOM */ |
| 55 | +React.renderComponent( |
| 56 | + <h1>Hello, world!</h1>, |
| 57 | + document.getElementById('example') |
| 58 | +); |
| 59 | +``` |
| 60 | + |
| 61 | +然后在 `helloworld.html` 引用该文件: |
| 62 | + |
| 63 | +```html{10} |
| 64 | +<script type="text/jsx" src="src/helloworld.js"></script> |
| 65 | +``` |
| 66 | + |
| 67 | +### 离线转换 |
| 68 | + |
| 69 | +先安装命令行工具(依赖 [npm](http://npmjs.org/)): |
| 70 | + |
| 71 | +``` |
| 72 | +npm install -g react-tools |
| 73 | +``` |
| 74 | + |
| 75 | +然后把你的 `src/helloworld.js` 文件转成标准的 JavaScript: |
| 76 | + |
| 77 | +``` |
| 78 | +jsx --watch src/ build/ |
| 79 | +
|
| 80 | +``` |
| 81 | + |
| 82 | +只要你修改了, `build/helloworld.js` 文件会自动生成。 |
| 83 | + |
| 84 | +```javascript{3} |
| 85 | +/** @jsx React.DOM */ |
| 86 | +React.renderComponent( |
| 87 | + React.DOM.h1(null, 'Hello, world!'), |
| 88 | + document.getElementById('example') |
| 89 | +); |
| 90 | +``` |
| 91 | + |
| 92 | +> 注意: |
| 93 | +> |
| 94 | +> 目前注释解析器还是非常严格;为了让它能够找出 `@jsx` 修饰符,有两个限制是必须的。`@jsx` 注释块必须是代码文件第一个注释。注释必须以 `/**` (`/*` 和 `//` 不起作用) 开头。如果解析器找不到 `@jsx` 注释,它就不会转换代码,直接原样输出到文件。 |
| 95 | +
|
| 96 | +对照下面更新你的 HTML 代码 |
| 97 | + |
| 98 | +```html{6,10} |
| 99 | +<!DOCTYPE html> |
| 100 | +<html> |
| 101 | + <head> |
| 102 | + <title>Hello React!</title> |
| 103 | + <script src="build/react.js"></script> |
| 104 | + <!-- No need for JSXTransformer! --> |
| 105 | + </head> |
| 106 | + <body> |
| 107 | + <div id="example"></div> |
| 108 | + <script src="build/helloworld.js"></script> |
| 109 | + </body> |
| 110 | +</html> |
| 111 | +``` |
| 112 | + |
| 113 | +## 想用 CommonJS? |
| 114 | + |
| 115 | +如果你想在一个模块系统里使用 React,[fork 我们的代码](http://github.com/facebook/react), `npm install` 然后运行 `grunt`。一个漂亮的 CommonJS 模块集将会被生成。我们的 `jsx` 转换工具可以很轻松的集成到大部分打包系统里(不仅仅是 CommonJS)。 |
| 116 | + |
| 117 | +## 下一步 |
| 118 | + |
| 119 | +去看看[入门教程](/react/docs/tutorial.html),然后学习其他在 `/examples` 目录里的示例代码。祝你好运,欢迎来到 React 的世界。 |
| 120 | + |
0 commit comments