React.jsでHello world!

そういえばやってなかったので、Hello worldしてみます。
Getting Started | React

参考:
React入門 | React 0.13 日本語リファレンス | js STUDIO
A JavaScript library for building user interfaces | React


スターターキットのルートディレクトリに次のコードを持つ、helloworld.htmlを作りましょう。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="build/react.js"></script>
    <script src="build/react-dom.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.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>

//ReactDOM.renderの第二引数のDOM要素の中に第一引数のHello world!が挿入されます。
JavaScript内部のXMLシンタックス(構文)はJSXと呼ばれます。JSX in Depth | ReactJSXをチェックして、それについてもっと学びましょう。

JSXをバニラのJavaScriptに変換するため、Reactではscript type="text/babel"を使い、babelを含めることで、ブラウザの中では実際に変換が行われるようになっています。

The XML syntax inside of JavaScript is called JSX; check out the JSX syntax to learn more about it. In order to translate it to vanilla JavaScript we use script type="text/babel" and include Babel to actually perform the transformation in the browser.

外部ファイル

Reactコードを別ファイルに移すこともできます。次のsrc/helloworld.jsを作りましょう。

Your React JSX code can live in a separate file. Create the following src/helloworld.js.

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


では、それをhelloworld.htmlから参照します。

Then reference it from helloworld.html:

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

HTTPを通してでないと、ファイルの読み込みに失敗するブラウザがあることに注意しましょう。(Chromeなど)

Note that some browsers (Chrome, e.g.) will fail to load the file unless it's served via HTTP.

オフライン変換

はじめにBabelコマンドラインツールをインストールします。(npmが必要です。)

First install the Babel command-line tools (requires npm):

npm install --global babel-cli
npm install babel-preset-react

次に、src/helloworld.jsファイルをバニラのJavaScriptに変換しましょう。

Then, translate your src/helloworld.js file to plain JavaScript:

babel --presets react src --watch --out-dir build

注意:もしあなたがES2015をお使いなら、 babel-preset-es2015 パッケージも使いましょう。

If you are using ES2015, you will want to also use the babel-preset-es2015 package.

変更を加えるたびに、自動的にbuild/helloworld.jsファイルが生成されます。発展的な使い方を学びたいなら、CLI · Babelを読みましょう。

The file build/helloworld.js is autogenerated whenever you make a change. Read the Babel CLI documentation for more advanced usage.

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

HTMLファイルを次のように更新しましょう。

Update your HTML file as below:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello React!</title>
    <script src="build/react.js"></script>
    <script src="build/react-dom.js"></script>
    <!-- No need for Babel! -->
  </head>
  <body>
    <div id="example"></div>
    <script src="build/helloworld.js"></script>
  </body>
</html>

次のステップ

チュートリアルと、スターターキットにあるexampleディレクトリで、もっと学びましょう。
私たちは、workflows,UI-components,routing,data management etc.を扱うwikiを所有しています。

Check out the tutorial and the other examples in the starter kit's examples directory to learn more.

We also have a wiki where the community contributes with workflows, UI-components, routing, data management etc.

Good luck, and welcome!