ASTRO Camp 7th
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 0504 React ###### tags: `framework` `JavaScript` [奶綠茶老師信箱](milkmidi@gmail.com) [上課連結](https://reurl.cc/OXyrp7) ## 環境設定: ### 加入 React emmet - 到 setting 介面 - 搜尋 "emmet" - 直接設定 item 跟 value - ![](https://i.imgur.com/A3OTKjW.png) - 直接開啟 setting (JSON) - 加入以下程式碼 ```json= "emmet.includeLanguages": { "javascript": "javascriptreact" } ``` ==再次強調== - 觀念很重要 - 下對關鍵字「找答案」 - 學會 React,年薪百萬,真心不唬爛 前端框架三強:jQuery / React / Vue / (Angular建議先不要碰) - React 公司比較多人用的原因 - pure framework - 標準的 JavaScript - 比Vue更早進入市場 Vue 有自己的 template 語法 --- ## virtual DOM 1. 前後端分離 2. 薪水高 3. 框架是前端工程師必備工具之一 4. 可以共同開發 ==沒有缺點== BUT 雅虎SEO不支援Virtual DOM react 會使用到 JSX 所以要裝 babel babel 工具:將 JSX 轉譯成瀏覽器看得懂的 JS(開發用所以效能差) --- ## [JSX](https://zh-hant.reactjs.org/docs/introducing-jsx.html) - JSX 長得像 HTML 但不是 HTML ```javascript= ReactDOM.render( element, //參數1,只能有一個根元素 document.getElementById('root') //參數2,將參數1塞進參數2 ); ``` ```javascript= ReactDOM.render( //這裡的 h1 是 JSX 的語法 <h1>Hello, world!!</h1>, document.getElementById('app') // 將virtual dom 放進 app 的 dom 元素裡面 ); ``` ### 多行原始碼 - 要用 div 把內容包起來 ```javascript= ReactDOM.render( <div> <h1>Hello, world!!</h1> <h2>John</h2> </div>, document.getElementById('app') ) ``` --- ## Component 元件 - Reusable 可重複使用 --- ## Emmet 設定 可以先使用 `CTRL` + `,` 或是 `CMD` + `,` 開啟VSCode設定頁面 settings.json ```json= "emmet.includeLanguages": { "javascript": "javascriptreact" } ``` 要在 JSX 中執行 JS 運算,要用大括號包起來 ```javascript= function FunctionalComponent(){ return ( <div className="functional-component"> 這是functionalComponent組件 <br /> 今天日期: { // 在 react 的 JSX 執行 JS 需要用花括號包起來 new Date().toDateString() } </div> ) } ``` - 要寫 React component 一定要大寫字母開頭 大寫駝峰式 - ==component 接受參數的唯一方法就是從 props 取得== ```javascript= ReactDOM.render( <div className="app"> <FunctionalCard img="http://fakeimg.pl/300x100/ecf0f1/" name="aa" /> // 如果沒有子元素可以自己結束掉 <FunctionalCard img="http://fakeimg.pl/240x80/ecf0f1/" name="aa"> <h1>children</h1> </FunctionalCard> // 如果有子元素就要包起來 </div> ``` - 從 prop 傳入的東西預設都是 string 型態 - 如果要傳入特殊型別的東西 ```javascript= function FunctionalCard(props){ const { img, name, children } = props return ( <div className="card"> <img className="img" src={img} /> <div className="name">name:{name}</div> <div className="quote">quote:{children}</div> </div> ) } ReactDOM.render( <div className="app"> <FunctionalCard img="https://picsum.photos/300/100/?random=1" name="milkmidi" /> <FunctionalCard img="https://picsum.photos/240/80/?random=1" name="John"> <h1>我是子元素</h1> </FunctionalCard> </div> , document.getElementById('app') ); ``` virtual DOM 有背後比對的機制 React 的 Component 設定趨近於封裝,自己的變數跟 function 只能在自己的 component 底下使用 useState 使用私有變數只能在 component 的範圍裡 ### React.useEffect 1. 每個component 都有生命週期:建立&死亡(前提是要塞空陣列) 2. 觀察有沒有異動 * 範例 07 ```javascript= function Clock() { React.useEffect(() => { console.log('componentDidMount') return () => { console.log('componentWillUnmount') } }, []) // ↑一定要加空陣列 return ( <div className="my-component"> <h2>react useEffect</h2> </div> ) } ``` react component render 發動條件 1. prop 改變 2. useState 裡面的 function 被發動 - 如果 useState 裡面的變數的記憶體位置沒有被更動的話就不會被發動 * [Array.prototype.concat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) - render 的時候可以用 array 或直接輸入,react 都可以解讀 - 範例 08 ```javascript= const numbers = [1, 2, 3, 4, 5]; const doubled = numbers.map((number) => number * 2); function List(){ const [list, setList] = React.useState(['學會 JS', '學會 React', '年薪百萬']) const atAddClick = () => { var newList = list.concat(new Date().toString()) setList(newList) } return ( <div> <button onClick={atAddClick}>Add</button> <ol className="list"> { list.map(function (text) { return <li key={text}>{text}</li> }) // react component 接受陣列跟直接輸入 } </ol> </div> ) } ReactDOM.render( <List />, document.getElementById('app') ); ``` - react virtual DOM render 的時候如果跑迴圈,一定要有一個不重複的 Key 值 - react useEffect 後面接的函式外層強制不能用非同步函式 - 但是如果外層寫同步但裡面包一個非同步函式是可以的 - useEffect 作用 - `[]`有塞變數時,偵測後面 Array 的值是否有異動(觀察變數使用) - `[]`是空的時候,在Array component 建立或死亡時發動函式(component 的生命週期) - virtual DOM render 完才發生(component 執行完) - setState 只是告訴 render 說它會在將來的某個時間點改變 - 不會即時更新 ref 找 DOM 元素 useState 模擬 this 奶綠的 coding style: 自己 component 的 私有方法:用 at 做前綴 props 傳進傳出的方法:用 on 做前綴

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully