Skip to content

Hello World

murajun1978 edited this page Jun 7, 2016 · 6 revisions

ReactでHello Worldヘ(^o^)ノ

プロジェクトを作成

  • ディレクトリを作成するだけ
$ mkdir es6-react-sample
$ cd es6-react-sample

package.jsonを作成

$ npm init -y

パッケージのインストール

  • npm(Node.jsのPackageManager)
$ npm i -S react react-dom
$ npm i -D webpack webpack-dev-server babel-loader babel-core babel-preset-es2015 babel-preset-react

Webpackの設定 (webpack.config.js)

module.exports = {
  entry: './main.js',
  output: {
    path: './',
    filename: 'index.js'
  },
  devServer: {
    inline: true,
    port: 5000
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  }
}

ページを作成 (index.html)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Hands-on with React.js and ES6</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="index.js"></script>
  </body>
</html>

Reactコンポーネントを作成 (App.js)

import React from 'react'

class App extends React.Component {
  render(){
    return <div>Hello World</div>
  }
}

export default App

エントリーポイントのファイルを作成 (main.js)

import React from 'react'
import ReactDom from 'react-dom'
import App from './App'

ReactDom.render(<App />, document.getElementById('app'))

package.jsonにスクリプトを追加 (package.js)

{
  "name": "es6-react-hands-on",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.1.0",
    "react-dom": "^15.1.0"
  },
  "devDependencies": {
    "babel-core": "^6.9.1",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  }
}

ディレクトリツリー

$ tree
.
|-- App.js
|-- README.md
|-- index.html
|-- main.js
|-- package.json
`-- webpack.config.js
Clone this wiki locally