内容简介:React 16.8 的 Hooks 橫空出世後,其 Functional Component 也能有 state 的寫法讓大家驚豔,React 從此可以不用寫 Class Component,朝 class-free 又邁進一大步。Node.js 8.12.0Create-react-app 2.1.3
React 16.8 的 Hooks 橫空出世後,其 Functional Component 也能有 state 的寫法讓大家驚豔,React 從此可以不用寫 Class Component,朝 class-free 又邁進一大步。
Version
Node.js 8.12.0
Create-react-app 2.1.3
React 16.8.1
安裝 Node.js
(略)
安裝 Yarn
(略)
安裝 Create-react-app 套件
$ yarn global add create-react-app
React 建置工具,用來建立 React 專案,相當於 Angular CLI、Vue CLI 地位。
建立 React 專案
$ npx create-react-app react-hello-world
使用 npx create-react-app
建立 React 專案。
- 建立成功後,最後會提示你進入專案目錄後,輸入
yarn start
啟動內建 Web Server。
啟動 Web Server
$ cd react-hello-world react-hello-world $ yarn serve
- React 啟動在
http://localhost:3000
- 目前為止,React 開發環境已經沒問題
Hello World
- React Hooks 在 Feb.06, 2019 的 16.8 正式成為 stable release,確認
package.json
的 React 版本為 16.8 之後
- 對於學習 React 而言,預設一些檔案是多餘的,可以刪除之,僅留下
src
目錄下App.js
與index.js
兩的檔案即可
src/index.js
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render(<App />, document.getElementById('root'));
將 CSS 與 Service Worker 部分刪除,只留下以上部分。
src/App.js
import React, { Component } from 'react'; class App extends Component { render() { return ( <div>Hello World</div> ); } } export default App;
刪除 SVG 與 CSS 部分, render()
只留下 <div>Hello World</div>
。
- 只剩下 Hello World,這是最簡單最乾淨的 React
Conclusion
- Create-react-app 讓 React 的學習門檻降低了,Webpack 之類的設定都會由 create-react-app 幫我們搞定
- React 16.8 為 Hooks 的 stable release,請確認 React 版本
- 將預設不必要的檔案刪除,以最乾淨的專案學習 React Hooks
Sample Code
完整範例可以在我的 GitHub 上找到
Reference
Reed Barger, React Hooks
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。