React 之 Hello World

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

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


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

查看所有标签

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

金字塔原理

金字塔原理

[美] 巴巴拉·明托 / 王德忠、张珣 / 民主与建设出版社 / 2002-12 / 39.80元

《金字塔原理》是一本讲解写作逻辑与思维逻辑的读物,全书分为四个部分。 第一篇主要对金字塔原理的概念进行了解释,介绍了如何利用这一原理构建基本的金字塔结构。目的是使读者理解和运用简单文书的写作技巧。 第二篇介绍了如何深入细致地把握思维的环节,以保证使用的语句能够真实地反映希望表达的思想要点。书中列举了许多实例,突出了强迫自己进行“冷静思维”对明确表达思想的重要性。 第三篇主要针对的......一起来看看 《金字塔原理》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

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

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具