Webpack + React 開發 01 HelloWorld

來源:互聯網
上載者:User

標籤:test   html   document   contain   pack   插入   logs   javascrip   from   

1.項目依賴

安裝所需要依賴的其它第三方開源庫,項目依賴如下:

  "dependencies": {    "babel-core": "^6.21.0",    "babel-loader": "^6.2.10",    "babel-preset-es2015": "^6.18.0",    "babel-preset-react": "^6.16.0",    "react": "^15.4.2",    "react-dom": "^15.4.2"  }

由於要使用到 import ,所以需要使用安裝babel-preset-es2015,其它的幾個庫都是必須的;

2.配置webpack.config.js
module.exports = {    entry: __dirname + ‘/app.js‘,    output: {        path: __dirname,        filename: ‘bundle.js‘    },    module: {        loaders: [            {                test: /\.js$/,                exclude: /node_modules/,                loader: ‘babel‘,                query: {                    presets: [‘es2015‘, ‘react‘]                }            }        ]    }}

主要就是配置相應的loader,在此只需要 es2015 和 react;

3.建立資源檔

index.html

<html>    <head>HelloWorld</head><body>    <div id="helloworldDiv"></div>    <script type="text/javascript" src="bundle.js"></script></body></html>

該網頁裡面只有一個 id 為 helloworldDiv 的 div,和引用的一個外部 script 檔案,該 script 檔案由webpack打包工具產生,而並非手動添加;

app.js

import React from ‘react‘;import {render} from ‘react-dom‘;import ReactDom from ‘react-dom‘;
// ReactDom.render(<h1>Hello World</h1>, document.getElementById(‘helloworldDiv‘));render(<h1>Hello World</h1>, document.getElementById(‘helloworldDiv‘));

在app.js,記錄了‘兩種‘渲染方法,實際上是同一種渲染方法;

可以通過ReactDom.render,也可以直接調用render方法,需要注意的是此兩種方法的區別:‘ReactDom‘ 是直接匯入的整個 ‘react-dom‘,而 ‘render‘ 則在使用 import 匯入的時候,添加了 ‘{}‘ 進行修飾,{} 修飾的對象為某一個具體的已知的方法,而沒有 {} 修飾的則為匯入的整個 ‘react-dom‘ 即 ReactDom,也就是說 render 是ReactDom對象的一個方法;

通過調試發現 ReactDom 對象結構如下:

Object {    findDOMNode    render(nextElement, container, callback)    unmountComponentAtNode    unstable_batchedUpdates    ...}

正好印證了上面的斷言;

4.Component

除了使用 

render(<h1>Hello World</h1>, document.getElementById(‘helloworldDiv‘));

這樣直接渲染一段組織好的html片斷之外,還可以以組件的形式來對html片斷進行封裝;

React 允許將代碼封裝成組件(component),然後像插入普通 HTML 標籤一樣,在網頁中插入這個組件。有兩種定義組件類的方法:

4.1 React.Component
class HelloWorld extends React.Component {    render() {        return (<div>Hello World</div>);    }}render(<HelloWorld />, document.getElementById(‘testDiv‘));

通過繼承 React.Component ,新建立一個組件類HelloWorld,在使用的時候,直接將該組件傳遞給render函數的nextElement參數;

4.2 React.createClass
var HelloWorld = React.createClass({  render: function() {    return <h1>Hello World</h1>;  }});

模板插入 <HelloWorld /> 時,會自動產生 HelloWorld 的一個執行個體。所有組件類都必須有自己的 render 方法,用於輸出組件。
注意,組件類的第一個字母必須大寫,否則會報錯,比如HelloWorld不能寫成helloWorld。另外,組件類render函數的返回只能包含一個頂層標籤,否則也會報錯。

4.3 無狀態組件

建立組件除了上面兩種方式外,還可以直接通過一個function來建立。

var React = require("react");var MyButton = function(props) {    return <div>        <button onClick={props.onClick}>New Item</button>    </div>;};module.exports = MyButton;// 無狀態組件定義方式

需要使用 require("react") 將 react匯入!

 

Webpack + React 開發 01 HelloWorld

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.