Hello ReactJS


前言

React學習前先搭好環境,官網的例子看着比較分散。結合webpack就可以體驗完整的es6開發流程了。

源碼:https://github.com/Ryan-Miao/hello-react-js/releases/tag/0.1

1. 環境搭建

涉及以下幾個技術。但從hello world的角度說,目前先不用知道是干嘛的,先用來學習react,后面再去研究各個組件的功能。

  1. Webpack - A module bundler
  2. Babel - A Javascript compiler
  3. ES6 - A relatively new Javasript standard
  4. Yarn - A package manager
  5. React - As expected

1.1 安裝一些東西

去官網下載NodeJS,安裝。
去官網下載yarn,然后安裝。

1.2 開始搭建

最終搭建的文件結構如下:

.
|____.babelrc
|____node_modules
|____app
| |____components
| | |____App.jsx
| |____index.html
| |____index.js
|____dist
| |____index.html
| |____index_bundle.js
|____package.json
|____readme.md
|____structure.txt
|____webpack.config.js
|____yarn.lock

1.2.1 初始化

mkdir hello-react-js
cd hello-react-js
yarn init

添加webpack

yarn add webpack webpack-dev-server path

這時,項目根目錄下會多出一個yarn.lock,不用理會。

1.2.2 在根目錄下創建webpack.config.js

var path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './app/index.html',
    filename: 'index.html',
    inject: 'body'
});

module.exports = {
    entry: './app/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'index_bundle.js'
    },
    module: {
        loaders: [
            { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
            { test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ }
        ]
    },
    plugins: [HtmlWebpackPluginConfig]
};

  • entry: 指向入口js文件
  • output: 指向打包后的文件目錄
    • filename: js打包后的文件名
    • path: 打包后的文件目錄
  • loaders: 轉換工具。這里簡單加載es6的轉換工具babel-loader,將以.js或者.jsx結尾的文件轉換為es5.
  • plugins: 一些插件。這里用到HtmlWebpackPlugin,將打包后的js文件插入到指定的html模板里。好處是我們不用手動將js插入html中,這在修改js文件名的時候很有用,否則我們還要手動修改js引入的名稱。

1.2.3 添加babel

剛才提到了babel-loader,除了配置之外還需要加載依賴:

yarn add babel-loader babel-core babel-preset-es2015 babel-preset-react --dev

然后,在根目錄創建.babelrc:

{
  "presets":[
    "es2015", "react"
  ]
}

1.2.4 添加react

yarn add react react-dom

創建app/index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>React App</title>
</head>
<body>
<div id="root">

</div>
</body>
</html>

創建app/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';

ReactDOM.render(<App />, document.getElementById('root'));

創建app/components/App.jsx

import React from 'react';

export default class App extends React.Component {
    render() {
        return (
            <div style={{textAlign: 'center'}}>
                <h1>Hello World! Hi ReactJS</h1>
            </div>);
    }
}

1.2.5 添加html-webpack-plugin

前面配置了html-webpack-plugin,這里還需要加載依賴文件:

yarn add html-webpack-plugin

在前面的配置文件制定了html模板文件,輸出文件名,以及js打包文件插入的位置。

1.3 基本搞定,運行一下

打開package.json。在script下添加

...
"scripts": {
    "start": "webpack-dev-server"
 },
 ...

運行

yarn start

這時候頁面顯示如下。大功告成。

可以在package.json里配置不同的webpack運行環境。這里只是搭建學習react官方文檔之前先要准備好的環境。最終,package.json:

{
  "name": "hello-react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack",
    "start": "webpack-dev-server",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Ryan Miao",
  "license": "MIT",
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "clean-webpack-plugin": "^0.1.16",
    "css-loader": "^0.28.4",
    "html-webpack-plugin": "^2.29.0",
    "less-loader": "^4.0.5",
    "react": "^15.6.1",
    "style-loader": "^0.18.2",
    "url-loader": "^0.5.9",
    "webpack": "^3.4.1",
    "webpack-dev-server": "^2.6.1"
  },
  "dependencies": {
    "path": "^0.12.7",
    "react-dom": "^15.6.1"
  }
}

1.3 小結

開始的時候照葫蘆畫瓢,先把環境搭建起來。然后開始學習,開始做事。在需要的時候去研究對應的問題。不然,知識何其多也。下面就可以照着react官網的教程,把react組件過一遍。然后再去看redux。

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM