Phoebe
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Transfer ownership
    • Delete this note
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    # useState ###### tags: `六角筆記王` [TOC] useState:用來設定 react 中的 state 語法如下: ```jsx= const [currentValue, setCurrentValue] = useState(initialValue); ``` - currentValue:存放 state 的值 - setCurrentValue:用來設定 state 值 - initialValue:state 的初始值 ## 先以計數器來說 以下面的範例來說,就是使用到`setCounter`來改變`counter`的值。 我們無法直接改變`counter`的值,只能藉由`setCounter`。 ```jsx= const [counter,setCounter] = useState(0) console.log('render',counter) const handleButtonClick = () =>{ console.log('button click!'); setCounter(counter+1); } return ( <div className="App"> <button onClick={handleButtonClick}>increment</button> {counter} </div> ) ``` 在下圖中可以注意到,再還沒按下`button`之前,`react`會先`render`一次。 ( redner 0 ) 每當點擊完`button`之後,`react`就`render`一次。 ![](https://i.imgur.com/GvKsm4B.png) ### 結論 由此可知,React是以「資料」驅動「畫面」進行更新。不需要再透過原生`JS`的`textContent`,`innerHTML`這樣的方式來更新HTML的畫面。 而現在畫面和元件的資料是關聯再一起的。因此,一旦資料改變了,React就會自動幫我們更新呈現的畫面。 ### JSX 需注意的點 在jsx裡面是無法使用迴圈的,因為沒有迴圈的概念。 不能在條件式(conditions),迴圈(loops) 或 嵌套函式(nested functions)中呼叫 Hook方法。 以useState來說,正確的寫法為 ```jsx= const Conuter = () =>{ const [count,setCount] = useState(); return{ {/* ... */} } } ``` 如果把useState放到if內可能會導致嚴重錯誤。 ```jsx= const Counter = () =>{ if(isValidCounter<=10){ const [count,setCount] = useState(); } return{ {/* ... */} }; } ``` 那為什麼會產嚴重錯誤呢? 原因是因為React Hooks中會去==記錄這些Hooks在函式中被呼叫的順序==,以確保資料能夠被相互對應。 但是,當我們將Hooks放到條件式或是迴圈內,就會==破壞==了這些Hooks被呼叫到的順序,因此造成錯誤。 ## 新增 todo 選項 所以這邊只能使用到`map`方法。 `map`與 forEach 非常類似,但是 `map` 會 return 一個值,並且產生一個**新陣列**出來。 需特別注意的是,下方第10行中,可能會想要寫`todos.push(777)`,但上面提過,todos會被視為相同的state,不能直接改state,它不會進行更新。 ```jsx= // 解構語法 import { useState } from 'react'; function App() { // 給他一個初始值 const [todos, setTodos] = useState([1]); const handleButtonClick = () => { // 傳入參數: 新的 todo + 解構 todos setTodos(["new todo", ...todos]); } return ( <div className="App"> /* 在 JSX 中,單標籤必須 /> 結尾 */ <input type="text" placeholder="Add todo..." /> <button onClick={handleButtonClick}>Add Todo</button> { /* 這邊加上key是因為React的規範 */ todos.map((todo, index) => <TodoItem key={index} content={todo} /> ) } </div> ); } ``` ![](https://i.imgur.com/81VJAoC.png) ## Controlled vs Uncontrolled 因為這邊也可以使用到`useRef`傳值,所以提一下。 在 React 中,表單元素的處理可分為 uncontrolled 和 controlled,兩者之間的差別,在==於 `component` 的資料是否受到 React 的控制==: ### Controlled component > 資料受到 React 的控制 如果將資料的控制權交給 React 來處理,畫面就會根據 state 是否改變來重新渲染。 ### Uncontrolled component > 資料不受 React 的控制 例如:input、textarea 等表單元素,通常會維持本身的 state,並根據使用者的輸入來更新該元素的 state。 若想取得 `uncontrolled` component 的值,可透過直接操作 DOM 或使用 useRef 來選取特定元素。 ### 簡單來說 input 值有無放到state裡面,如果有的話,就是`controll`,沒有的話就是`uncontrolled`。 ## 新增 todo 選項裡的 value ```jsx= const [todos,setTodos] = useState([]); const [value,setVaule] = useState(''); const InputValueHandler = e => { setVaule(e.target.value); }; const ButtonClickHandler = e =>{ setTodos([value, ...todos]); setVaule('') } return( <div className="App"> <input type="text" placeholder="todo" value={value} onChange={InputValueHandler}/> <button onClick={ButtonClickHandler}>Add Todo</button> { todos.map((todo,index) => <TodoItem content={todo} key={index} value={value} onChange={InputValueHandler}/>) } </div> ) ``` 可以觀察到value就寫進去了。 ![](https://i.imgur.com/mbhvpR8.png) ## 新增 todo 選項裡 自帶 id ```jsx= import './App.css'; import ToDoItem from './components/ToDoItem'; import { useState, useEffect, useRef } from 'react'; function App() { const [todos, setTodos] = useState([]); const [value, setVaule] = useState(''); // 使用useRef將id傳進去 const id = useRef(2); const InputValueHandler = e => { setVaule(e.target.value); }; const ButtonClickHandler = () => { // id 與 content 的部分 setTodos([{ id: id.current, content: value, }, ...todos]); // 輸入框清空 setVaule(''); id.current++; } return ( <div className="App"> <input type="text" placeholder="todo" value={value} onChange={InputValueHandler} /> <button onClick={ButtonClickHandler}>Add Todo</button> { // 將整個 todo 傳入 ToDoItem component todos.map((todo) => <ToDoItem todo={todo} onChange={InputValueHandler} />) } </div> ) } export default App; ``` ### ToDoItem 元件 ```jsx= import '../App.css'; import styled from 'styled-components'; const TodoItemWrapper = styled.div` display:flex; align-items:center; justify-content: space-between; padding: 8px 16px; border: 1px solid black; width:600px; margin: 0 auto; `; const TodoContent = styled.div` color:green; font-size: ${props => props.size === "S" ? '20px' : '12px'}; `; const TodoButtonWrapper = styled.div` `; const Button = styled.button` padding:4px; color:black; &:hover{ color:red; } &+& { margin-left:4px; } `; const RedButton = styled(Button)` color:red; `; // 父元件傳過來的 props.todo export default function ToDoItem({ todo }) { return ( // 將id帶入 <TodoItemWrapper data-todo-id={todo.id}> <TodoContent> {todo.content} </TodoContent> <TodoButtonWrapper> <Button>已完成</Button> <RedButton>刪除</RedButton> </TodoButtonWrapper> </TodoItemWrapper> ) }; ``` ## 刪除功能 刪除功能想起來很複雜,但實作相對於其他功能是容易的。 為以下步驟, 1. 將 function 寫在 Parent,並傳入參數(id) 2. 再由 Children 呼叫 Parent 3. 在 Parent 處理 function ### 1 將 function 寫在 Parent,並傳入參數(id) ```jsx= const handleDeleteTodo = id => { } return ( <div className="App"> <input type="text" placeholder="todo" value={value} onChange={InputValueHandler} /> <button onClick={ButtonClickHandler}>Add Todo</button> { todos.map((todo) => <ToDoItem todo={todo} onChange={InputValueHandler} handleDeleteTodo={handleDeleteTodo} />) } </div> ) ``` ### 2 再由 Children 呼叫 Parent ```jsx= // 傳入props export default function ToDoItem({ todo, handleDeleteTodo }) { return ( <TodoItemWrapper data-todo-id={todo.id}> <TodoContent> {todo.content} </TodoContent> <TodoButtonWrapper> <Button>已完成</Button> {/* 將handleDeleteTodo函式傳入 */} <RedButton onClick={() => { handleDeleteTodo(todo.id) }}>刪除</RedButton> </TodoButtonWrapper> </TodoItemWrapper> ) ``` ### 3 在 Parent 處理 function ```jsx= // 把有這個id的item利用filter去掉 const handleDeleteTodo = id => { setTodos(todos.filter(todo => todo.id !== id)) } ``` ## 編輯todo功能 在開始實做之前,可以先將預設值帶入,在物件中加入`isDone`的狀態,來判斷是否已經完成。 ```jsx= function App() { const [todos, setTodos] = useState([ { id: 1, content: 'done', isDone: true }, { id: 2, content: 'not done', isDone: false } ]); const [value, setVaule] = useState(''); const id = useRef(2); ``` 這裡分為兩個步驟, 1. 在 Parent 建立函式,傳入 Children 2. Children 寫好編輯後的 css,當isDone參數變化時,帶入不同css ## 1 在 Parent 建立函式,傳入 Children ```jsx= // 修改使用map,刪除用filter,新增使用解構語法 const handleToggleDone = id => { setTodos(todos.map(todo => { // 回傳原本的todo if (todo.id !== id) return todo // 原本的todo + 要修改的屬性 return { ...todo, isDone: !todo.isDone } })) } ``` ## 2 當isDone參數變化時,帶入不同css ```jsx= const TodoContent = styled.div` color:green; ${props => props.$isDone && ` text-decoration: line-through `}; `; ``` ```jsx= export default function ToDoItem({ todo, handleDeleteTodo, handleToggleDone }) { return ( <TodoItemWrapper data-todo-id={todo.id}> {/* 加上isDone props,在前方加上$,可以使它變成style component */} <TodoContent $isDone={todo.isDone}> {todo.content} </TodoContent> <TodoButtonWrapper> {/* inline-function */} <Button onClick={() => { handleToggleDone(todo.id) }}> {/* 使用三元運算子進行判斷 */} {todo.isDone ? '已完成' : "未完成"} </Button> <RedButton onClick={() => { handleDeleteTodo(todo.id) }}>刪除</RedButton> </TodoButtonWrapper> </TodoItemWrapper> ) ``` 除了使用三元運算子進行判斷,也可以使用邏輯運算子 `&&` ```jsx= <Button onClick={handleToggleClick}> {todo.isDone && '已完成'} {!todo.isDone && '未完成'} </Button> ``` ### 邏輯運算子的使用:`&&` 和 `||` > 當 `||` 前面的值為 false 時,就取後面的值。 ```jsx= const a = 0 || 'iPhone'; // 0轉型後為false,所以 a 是iphone const b = true || "不會輪到我"; // 因為true為真,所以b是true。 ``` > 當 `&&` 前面的值為 true 時,就取後面的值。 ```jsx= const a = 0 && 'iPhone'; // 0轉型後為false,所以 a 是 0 const b = true && "會輪到我"; // 因為true為真,所以b是會輪到我。 ``` 所以計數器的判斷式可以改成 ```jsx= { count < 10 && ( <div className="chevron chevron-up" onClick={()=> setCount(count+1)}/> ); } ``` ### Transient props:`$<props>` 在上方程式碼中,加在 `TodoContent` 的 `$isDone` 這個 props,會被視為 style component props,不會被繼續傳到下一個 DOM 元素,也就不會顯示在 `TodoContent` 標籤上。 如果沒有加上 `$` 符號,這個 props 就會被直接加在 `TodoContent` 這個 DOM 結構上。 再以下方程式碼為例: ```jsx= <TodoContent id="hello" $isDone={todo.isDone}>{todo.content}</TodoContent> ``` 可以發現經過 render 之後,在 DOM 元素只會出現 `id="hello"` 這個屬性,而不會有 `$isDone`,這是因為 Transient props 不會被往下傳。 ![](https://i.imgur.com/s1U7X17.png) ## 結論 React 和以往的思考模式其實很不一樣,像是在切好的 UI 畫面上新增各種功能;而 React 則是先==思考 state 狀態==,再去想會如何改變畫面。 記住一個重點,就是 Component 之間可透過 props 把 state 傳遞下去。並且,只要 state 所有變動,就會觸發 render() 來更新 UI 畫面。

    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