2021-09-28|閱讀時間 ‧ 約 2 分鐘

Getting Started with React Hooks

Create React App 提供了快速建立React App環境的方法:
1. 安裝node.js 2. 建立React project:
$ npx create-react-app my-app
3. 啟動app
$ cd my-app
$ npm start
npm start之後即打開http://localhost:3000/網頁,每次修改都會直接refresh網頁.若是用VSCode開發, 直接從NPM Scripts用按的也很方便。 上線時使用npm run build,然後把build資料夾下的所有檔案丟到server即可。

App.js:
App.css:
最後build出來的class name如下:
將App.css改成App.module.css:
好處是在不同檔案的css class name可以取成一樣,不用擔心會有衝突,因為最後build出來的class name會編譯成不重複的名稱。
Note:
  • 這是用React Hook的寫法(function component),比class component的寫法簡潔許多。
  • 若是從git上剛clone下來,記得下npm install安裝相關的dependencies。
分享至
成為作者繼續創作的動力吧!
小弟是一位軟體工程師,樂於幫助他人,撰寫技術文章除了幫助自己複習以外,也希望可以幫助到他人,若文章內容有誤,還請大大不吝給予指教!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言