Files
react.dev/docs/getting-started.zh-CN.md
Reed Loden dd010b34e2 SSL/TLSize all the things! (convert http:// to https:// where appropriate)
Update links to use https:// where it is supported. There's probably a lot
more that could be fixed, but these are the core ones I found (especially
the download links in order to prevent MITM attacks). Note that there are
some fb.me links that will redirect to http:// even while accessed over
https://, but this seemed like the best way to fix those for now.

NOTE: Only non-third-party files were modified. There are references to
http:// URLs in vendored/third-party files, but seems appropriate to fix
upstream for those rather than editing the files.

Also, copy one image locally to the blog, as it was hotlinking to a site
that did not support https://.

Last, use youtube-nocookie.com instead of youtube.com for video embeds,
as the former doesn't try to set a cookie on load (privacy enhancement).
2015-04-18 16:49:32 -07:00

3.2 KiB
Raw Blame History

id, title, permalink, next, redirect_from
id title permalink next redirect_from
getting-started-zh-CN 入门教程 getting-started-zh-CN.html tutorial-zh-CN.html docs/index-zh-CN.html

JSFiddle

开始 Hack React 的最简单的方法是用下面 JSFiddle 的Hello Worlds

入门教程包 (Starter Kit)

开始先下载入门教程包。

在入门教程包的根目录,创建一个含有下面代码的 helloworld.html

<!DOCTYPE html>
<html>
  <head>
    <script src="build/react.js"></script>
    <script src="build/JSXTransformer.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/jsx">
      React.render(
        <h1>Hello, world!</h1>,
        document.getElementById('example')
      );
    </script>
  </body>
</html>

在 JavaScript 代码里写着 XML 格式的代码称为 JSX可以去 JSX 语法 里学习更多 JSX 相关的知识。为了把 JSX 转成标准的 JavaScript我们用 <script type="text/jsx"> 标签包裹着含有 JSX 的代码,然后引入 JSXTransformer.js 库来实现在浏览器里的代码转换。

分离文件

你的 React JSX 代码文件可以写在另外的文件里。新建下面的 src/helloworld.js

React.render(
  <h1>Hello, world!</h1>,
  document.getElementById('example')
);

然后在 helloworld.html 引用该文件:

<script type="text/jsx" src="src/helloworld.js"></script>

离线转换

先安装命令行工具(依赖 npm

npm install -g react-tools

然后把你的 src/helloworld.js 文件转成标准的 JavaScript:

jsx --watch src/ build/

只要你修改了, build/helloworld.js 文件会自动生成。

React.render(
  React.createElement('h1', null, 'Hello, world!'),
  document.getElementById('example')
);

对照下面更新你的 HTML 代码

<!DOCTYPE html>
<html>
  <head>
    <title>Hello React!</title>
    <script src="build/react.js"></script>
    <!-- 不需要 JSXTransformer -->
  </head>
  <body>
    <div id="example"></div>
    <script src="build/helloworld.js"></script>
  </body>
</html>

想用 CommonJS

如果你想在 browserifywebpack 或者或其它兼容CommonJS的模块系统里使用 React只要使用 react npm 包 即可。而且,jsx 转换工具可以很轻松的地集成到大部分打包系统里(不仅仅是 CommonJS

下一步

去看看入门教程 和入门教程包 examples 目录下的其它例子学习更多。

我们还有一个社区开发者共建的 Wikiworkflows, UI-components, routing, data management etc.

恭喜你,欢迎来到 React 的世界。