React 之 Hello World

栏目: IOS · Android · 发布时间: 5年前

内容简介: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 之 Hello World

建立 React 專案

$ npx create-react-app react-hello-world

使用 npx create-react-app 建立 React 專案。

React 之 Hello World

React 之 Hello World

  1. 建立成功後,最後會提示你進入專案目錄後,輸入 yarn start 啟動內建 Web Server。

啟動 Web Server

$ cd react-hello-world
react-hello-world $ yarn serve

React 之 Hello World

  1. React 啟動在 http://localhost:3000

React 之 Hello World

  • 目前為止,React 開發環境已經沒問題

Hello World

React 之 Hello World

  • React Hooks 在 Feb.06, 2019 的 16.8 正式成為 stable release,確認 package.json 的 React 版本為 16.8 之後

React 之 Hello World

  1. 對於學習 React 而言,預設一些檔案是多餘的,可以刪除之,僅留下 src 目錄下 App.jsindex.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 部分刪除,只留下以上部分。

React 之 Hello World

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>

React 之 Hello World

React 之 Hello World

  • 只剩下 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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

实用语义网

实用语义网

2009-2 / 59.00元

《实用语义网RDFS与OWL高效建模(英文版)》是语义网的入门教程,详细讲述语义网的核心内容的语言,包括语义网的概念、语义建模等。语义网的发展孕育着万维网及其应用的一场革命,作为语义网核心内容的语言:RDF和OWL,逐渐得到广泛的重视和应用。 《实用语义网RDFS与OWL高效建模(英文版)》对于任何对语义网感兴趣的专业技术人员都是十分难得的参考书。一起来看看 《实用语义网》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码