ReactでHello World!
Node.jsのインストール
Homebrewでインストール
brew -v brew update brew install nodejs node -v
インストール用プロジェクトの作成
JSのプログラムをプロジェクト単位で管理。
npm init -y でパッケージの管理ファイル(package.json)を作成する。
mkdir hello_react cd hello_react npm init -y
package.jsonの変更
{
"name": "hello_react",
"version": "1.0.0",
"description": "Hello React", #1
"private": true, #2
"main": "index.js",
"scripts": {
"start": "webpack-dev-server", #3
"webpack": "webpack -d" #4
},
"keywords": [],
"author": "",
"license": "ISC"
}
npmパッケージをインストール
npm install react react-dom npm install webpack webpack-cli webpack-dev-server --save-dev npm install babel-cli babel-loader babel-preset-env babel-preset-react --save-dev npm install eslint eslint-loader eslint-plugin-react --save-dev npm install css-loader style-loader --save-dev
インストール結果の確認用Reackコード
- ディレクトリー作成
mkdir src mkdir public
- .babelrc作成
vim .babelrc
cat .babelrc
{
"presets": ["env", "react"]
}
- .eslintrc.json作成
vim .eslintrc.json
cat .eslintrc.json
{
"env": {
"browser": true,
"es6": true
},
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true
}
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"plugins": ["react"],
"rules": {
"no-console": "off"
}
}
- webpack.config.js作成
vim webpack.config.js
cat webpack.config.js
module.exports = {
entry: {
app: "./src/index.js"
},
output: {
path: __dirname + '/public/js',
filename: "[name].js"
},
devServer: {
contentBase: __dirname + '/public',
port: 8080,
publicPath: '/js/'
},
devtool: "eval-source-map",
mode: 'development',
module: {
rules: [{
test: /\.js$/,
enforce: "pre",
exclude: /node_modules/,
loader: "eslint-loader"
}, {
test: /\.css$/,
loader: ["style-loader","css-loader"]
}, {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}]
}
};
- public/index.html作成
vim public/index.html cat public/index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="X-UA-Compatible" content="IE=Edge, chrome=1" /> <title>React App</title> </head> <body> <div id="root"></div> <script type="text/javascript" src="js/app.js" charset="utf-8"></script> </body> </html>
- src/index.js
vim src/index.js
cat src/index.js
import React from 'react'
import ReactDOM from 'react-dom'
ReactDOM.render(
<h1>Hello, world!!</h1>,
document.getElementById('root')
)
- 確認
npm start
ターミナルに webpack: Compiled successfully. が表示されたら ブラウザーで
http://localhost:8080 をアクセスしてHello World! と表示されたらOK.