RZ-Huang
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # React 的 form 表單實作 ###### tags: `javascript`、`react` ### 拿到表單中的值並呈現 第一種方式是叫做 Uncontrolled Components: ```jsx= import React, { Component } from 'react' class App extends Component { constructor(props) { super(props) this.input = React.createRef() // 加這行 input 的 method,讓 call 它的標籤都會創造一個 reference this.handleSubmit = this.handleSubmit.bind(this) } handleSubmit(e) { alert(this.input.current.value) // 從有 ref 的地方中拿到它們的值 e.preventDefault() } render() { return ( <div className="App"> <form onSubmit={this.handleSubmit}> <div> // 為這個 input 建立一個 ref 姓名: <input name="name" type="text" ref={this.input} /> </div> <div> 地址: <input name="address" type="text" /> </div> <div> 心得: <textarea name="review" /> </div> <div> 性別:<input id="gender_male" type="radio" value="male" name="gender"></input> <label for="gender_male">男</label> <input id="gender_female" type="radio" value="female" name="gender"></input> <label for="gender_female">女</label> <input id="gender_other" type="radio" value="other" name="gender"></input> <label for="gender_other">其他</label> </div> <div> 有空的時間:<input id='time_mon' type="checkbox" value="1" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_mon'>星期一</label> <input id='time_tue' type="checkbox" value="2" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_tue'>星期二</label> <input id='time_wed' type="checkbox" value="3" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_wed'>星期三</label> </div> <div> <input type="submit" /> </div> </form> </div> ) } } export default App ``` 因為第一種方式變成說 state 當中沒有值和 UI 對應,也就是說使用者打了什麼會不知道,不太符合 React 的原則。 第二種方式則是 Controlled Components,簡單說就是使用 state 的機制來儲存表單的值,程式碼如下: ```jsx= import React, { Component } from 'react' class App extends Component { constructor(props) { super(props) // state 分別加上每個 input 相對應的名稱 this.state = { name: 123, address: '', review: '' } this.handleSubmit = this.handleSubmit.bind(this) this.handleInputChange = this.handleInputChange.bind(this) } handleSubmit(e) { console.log(this.state) e.preventDefault() } // 每當表單有動作就負責改變 state 的值 handleInputChange(e) { this.setState({ /* 這邊使用 [] 括起來代表說它的值是可變的 根據你的 e.target.name 選到哪個表單 name 而成為什麼 這樣寫的好處就是所有表單只要一個 onChange 事件監聽的 function 就可以改變每個表單的 state 的值了 */ [e.target.name]: e.target.value }) } render() { const { name, address, review } = this.state return ( <div className="App"> /* 先替前面三個 input 加入 value 屬性和 onChange 事件監聽 value 屬性的值就是與 state 做連接,每當 state 一改變,value 跟著變 onChange 就是每次表單的值一更動就會觸發的事件監聽 然後都是指向 handleInputChange 這個 function */ <form onSubmit={this.handleSubmit}> <div> 姓名: <input name="name" type="text" value={name} onChange={this.handleInputChange} /> </div> <div> 地址: <input name="address" type="text" value={address} onChange={this.handleInputChange} /> </div> <div> 心得: <textarea name="review" value={review} onChange={this.handleInputChange} /> </div> <div> 性別:<input id="gender_male" type="radio" value="male" name="gender"></input> <label htmlFor="gender_male">男</label> <input id="gender_female" type="radio" value="female" name="gender"></input> <label htmlFor="gender_female">女</label> <input id="gender_other" type="radio" value="other" name="gender"></input> <label htmlFor="gender_other">其他</label> </div> <div> 有空的時間:<input id='time_mon' type="checkbox" value="1" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_mon'>星期一</label> <input id='time_tue' type="checkbox" value="2" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_tue'>星期二</label> <input id='time_wed' type="checkbox" value="3" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_wed'>星期三</label> </div> <div> <input type="submit" /> </div> </form> </div> ) } } export default App ``` ### 繼續幫其他表單元件加上事件監聽與拿取值 加上 state 之前,表單再新增一個 `<select>` 標籤選擇城市,然後放在表單最下面: ```jsx= <div> 城市:<select name="city"> <option value="taipei">台北市</option> <option value="new_taipei">新北市</option> <option value="other">其他</option> </select> </div> ``` 接著替 `type=radio` 的 input 加上: ```jsx= <div> 性別:<input id="gender_male" type="radio" value="male" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_male">男</label> <input id="gender_female" type="radio" value="female" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_female">女</label> <input id="gender_other" type="radio" value="other" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_other">其他</label> </div> ``` 可以沿用 `handleInputChange` 這個 function,因為提取表單的值的語法是一樣的。記得在 `state` 加上 `gender: ''` 初始值。 不過如果今天 `gender` 這個 state 有初始值的話,在 `type=radio` 的表單中相對應的「圈圈」不會是被點到的狀態,像這樣都沒有被圈: ![](https://i.imgur.com/6AP9xQc.png) 因此我們還得為這個表單加上 `checked` 的屬性: ```jsx= <div> 性別:<input id="gender_male" checked={gender === 'male'} type="radio" value="male" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_male">男</label> <input id="gender_female" checked={gender === 'female'} type="radio" value="female" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_female">女</label> <input id="gender_other" checked={gender === 'other'} type="radio" value="other" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_other">其他</label> </div> ``` 接著替 `select` 加上: ```jsx= <div> 城市:<select name="city" value={city} onChange={this.handleInputChange}> <option value="taipei">台北市</option> <option value="new_taipei">新北市</option> <option value="other">其他</option> </select> </div> ``` 跟上面都是一樣的做法,也是用 `handleInputChange` 這個 function 來變更 state。也要記得在 `state` 加上 `city: ''` 初始值,不過有個小瑕疵是一開始的 `select` 是在 `台北市` 的位置,但是 `state` 上依然是初始值:`''`,我們可以直接把在 `select` 選項當中放在第一個的 `台北市` 的值,也就是 `taipei` 直接設定成 `city` 的初始值,這樣就可以解決了。 接著替 `type=checkbox` 加上: ```jsx= handleCheckboxChange(e) { const { time } = this.state const value = e.target.value const newTime = time.filter(item => item !== value) this.setState({ time: time.length !== newTime.length ? newTime : [...time, value] }) } ``` 先在 `state` 裡面新增 `time: []`,因為「有空的時間」這個表單可以是複數值,因此我們使用陣列來儲存複數值。而上面那段的 `newTime` 就是先過濾等於 `value` 的值,剩下的值成為新的 `time` state,而這個 `value` 值要嘛就是被打勾的,要嘛就是被取消打勾的,因此我們接著看到 `setState` 裡面那段判別式,意思就是說原先的 `time` 的陣列長度和被過濾後的 `newTime`的陣列長度如果不一樣,那就把 `time` 更新成被過濾後的 `newTime`,而前後兩個陣列的長度不一樣只有在一個情況會發生,那就是有值被過濾的時候(就是有值被取消打勾了)。 如果都沒被過濾就表示 `newTime` 裡面的值總數就如同原先的 `time` state,因為根本沒動過,所以前後兩個陣列長度會是一樣的,判別式中,如果「陣列長度不一樣」不成立,換句話說陣列長度就是一樣的,而沒被過濾就表示原先的 `time` 陣列並不存在 `value` 值,也表示 `value` 需要被新增到 `time` 當中(就是被打勾的),所以使用 `[...time, value]` 的方式在原先 `time` 陣列當中新增了 `value` 值。 不過我們還要再加上當 `state` 中的 `time` 有初始值的時候,`checkbox` 相對應的「框框」需要被打勾,這時就要設定 `checked` 的屬性,像這樣: ```jsx= <div> 有空的時間:<input id='time_mon' checked={time.indexOf('1') >= 0} type="checkbox" value="1" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_mon'>星期一</label> <input id='time_tue' checked={time.indexOf('2') >= 0} type="checkbox" value="2" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_tue'>星期二</label> <input id='time_wed' checked={time.indexOf('3') >= 0} type="checkbox" value="3" name="time" onChange={this.handleCheckboxChange} /> <label htmlFor='time_wed'>星期三</label> </div> ``` 為每個 `type=checkbox` 的表單加上 `checked={time.indexOf('1') >= 0}`,其中的 `indexOf` 就是要尋找在參數面的那個值到底在 `time` 陣列的哪個位置,如果不在陣列的任何位置的話,就會回傳 `-1`,因此才有那個 `>=0` 的判別式。 ### 做優化 來幫上面 `type=checkbox` 的表單做優化,因為太長了,所以我們可以把它獨立出去成一個 Component,如下: ```jsx= const Checkbox = ({ id, value, htmlFor, label, checked, onChange }) => ( <span> <input id={id} checked={checked} type="checkbox" value={value} name="time" onChange={onChange} /> <label htmlFor={htmlFor}>{label}</label> </span> ) ``` 要用一個元素包起來全部的內容,這邊是使用 `<span>`。然後也使用了 functional component,而命名函式有省略 `{}` 和 `return`,直接用 `()` 包起來全部要 return 的內容。 另外原先在 Parent component 位置的元素就會變成這樣: ```jsx= <div> 有空的時間: <Checkbox id='time_mon' value='1' htmlFor='time_mon' label='星期一' checked={time.indexOf('1') >= 0} onChange={this.handleCheckboxChange} /> <Checkbox id='time_tue' value='2' htmlFor='time_tue' label='星期二' checked={time.indexOf('2') >= 0} onChange={this.handleCheckboxChange} /> <Checkbox id='time_wen' value='3' htmlFor='time_wen' label='星期三' checked={time.indexOf('3') >= 0} onChange={this.handleCheckboxChange} /> </div> ``` 而可以依樣畫葫蘆,其他的表單型式也可以獨立出去成為另一個 Children component。 ### 所有內容 把前面所改過的內容的程式碼與初始 UI 根據 `state` 的值所呈現的畫面附上: ```jsx= import React, { Component } from 'react' const Checkbox = ({ id, value, htmlFor, label, checked, onChange }) => ( <span> <input id={id} checked={checked} type="checkbox" value={value} name="time" onChange={onChange} /> <label htmlFor={htmlFor}>{label}</label> </span> ) class App extends Component { constructor(props) { super(props) this.state = { name: 123, address: '444', review: '5123', gender: 'male', city: 'new_taipei', time: ['1'] } this.handleSubmit = this.handleSubmit.bind(this) this.handleInputChange = this.handleInputChange.bind(this) this.handleCheckboxChange = this.handleCheckboxChange.bind(this) } handleSubmit(e) { console.log(this.state) e.preventDefault() } handleInputChange(e) { this.setState({ [e.target.name]: e.target.value }) } handleCheckboxChange(e) { const { time } = this.state const value = e.target.value const newTime = time.filter(item => item !== value) this.setState({ time: time.length !== newTime.length ? newTime : [...time, value] }) } render() { const { name, address, review, gender, city, time } = this.state return ( <div className="App"> <form onSubmit={this.handleSubmit}> <div> 姓名: <input name="name" type="text" value={name} onChange={this.handleInputChange} /> </div> <div> 地址: <input name="address" type="text" value={address} onChange={this.handleInputChange} /> </div> <div> 心得: <textarea name="review" value={review} onChange={this.handleInputChange} /> </div> <div> 性別:<input id="gender_male" checked={gender === 'male'} type="radio" value="male" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_male">男</label> <input id="gender_female" checked={gender === 'female'} type="radio" value="female" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_female">女</label> <input id="gender_other" checked={gender === 'other'} type="radio" value="other" name="gender" onChange={this.handleInputChange}></input> <label htmlFor="gender_other">其他</label> </div> <div> 有空的時間: <Checkbox id='time_mon' value='1' htmlFor='time_mon' label='星期一' checked={time.indexOf('1') >= 0} onChange={this.handleCheckboxChange} /> <Checkbox id='time_tue' value='2' htmlFor='time_tue' label='星期二' checked={time.indexOf('2') >= 0} onChange={this.handleCheckboxChange} /> <Checkbox id='time_wen' value='3' htmlFor='time_wen' label='星期三' checked={time.indexOf('3') >= 0} onChange={this.handleCheckboxChange} /> </div> <div> 城市:<select name="city" value={city} onChange={this.handleInputChange}> <option value="taipei">台北市</option> <option value="new_taipei">新北市</option> <option value="other">其他</option> </select> </div> <div> <input type="submit" /> </div> </form> </div> ) } } export default App ``` ![](https://i.imgur.com/qz6dmOX.png) 表單初始內容就是 `state` 設定的初始值,而有了 `state` 的值,我們操作其它功能就比較方便,比如表單驗證。

    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