การสร้างฟอร์ม HTML ใน React

     Element ของฟอร์ม HTML จะทำงานแตกต่างจาก Element DOM อื่นๆ ใน React เพราะว่า Elements ของฟอร์มมีความเป็นธรรมชาติมากกว่า

     Exanple ฟอร์มในรูปแบบ HTML ธรรมดา :

<form>
  <label>
    Name:
    <input type="text" name="name" />
  </label>
  <input type="submit" value="Submit" />
</form>

     จากข้างบน ฟอร์มนี้มีรูปแบบ HTML การทำงานของมันคือ จะแสดงข้อมูลบนหน้าเว็บเมื่อเราส่งแบบฟอร์ม แต่เราจะใช้ฟังก์ชัน JavaScript มาจัดการกับการส่งแบบฟอร์มและเข้าถึงข้อมูลที่เรากรอกลงในแบบฟอร์ม

 

Component ที่ใช้งานอยู่

     ใน HTML ฟอร์ม Element จะใช้ <input>, <textarea> และ <select> ฟอร์มเหล่านี้ จะมีการอัพเดทค่า เมื่อเรากำหนดค่าให้มัน โดยใช้ setState() เราสามารถรวมทั้งสองไว้ได้โดยการทำให้ State เป็น "single source of truth" จากนั้น Component ที่สร้างฟอร์มจะควบคุมสิ่งที่เกิดขึ้นในแบบฟอร์ม Element แบบป้อนข้อมูลที่มีค่าถูกควบคุมโดย React ในลักษณะนี้เรียกว่า "component ควบคุม"

     Exanple  :

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

     จากตัวอย่าง เมื่อ Value ถูก set ค่าโดยฟอร์ม Element ค่าที่แดสงออกมาจะเป็น this.state.value เนื่องจากคำสั่ง handChange จะทำงานเมื่อเราพิมพ์ข้อมูลต่างๆ เพื่ออัพเดทสถานะ ค่าที่แสดงออกมาจะเป็นค่าตามที่เราได้พิมพ์ออกมา

 

Textarea tag

     ใน HTML <textarea> จะกำหนดให้ใส่เป็นข้อความ เช่น :

<textarea>
  Hello there, this is some text in a text area
</textarea>

     ส่วนใน React <textarea> จะใช้ค่าเป็น value แทน วิธีนี้ฟอร์มที่ใช้ <textarea> สามารถเขียนได้เหมือน ฟอร์มที่ใช้อินพุตแบบบรรทัดเดียว :

class EssayForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: 'Please write an essay about your favorite DOM element.'
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('An essay was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <textarea value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

     สังเกตว่า this.state.value ถูกเตรียมให้ใช้งานใน Constructor เพื่อให้ข้อความเริ่มต้นด้วยข้อความบางส่วนในไฟล์

 

Select tag

     ใน HTML <select> จะสร้าง Drop-down ดังตัวอย่าง :

<select>
  <option value="grapefruit">Grapefruit</option>
  <option value="lime">Lime</option>
  <option selected value="coconut">Coconut</option>
  <option value="mango">Mango</option>
</select>

     จากตัวอย่าง Coconut ได้ถูกเลือกให้เป็นค่าเริ่มต้น เพราะว่าเราใส่คำสั่ง selected ไว้ใน option เพราะว่า React ใช้แอตทริบิวต์ value ในแท็ก select root ในส่วนควบคุมเนื่องจากเราจำเป็นต้องอัปเดตในครั้งเดียว

     Example :

class FlavorForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: 'coconut'};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('Your favorite flavor is: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Pick your favorite La Croix flavor:
          <select value={this.state.value} onChange={this.handleChange}>
            <option value="grapefruit">Grapefruit</option>
            <option value="lime">Lime</option>
            <option value="coconut">Coconut</option>
            <option value="mango">Mango</option>
          </select>
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

     แท็กทั้งหมดที่กล่าวมานี้ <input type = "text"> <textarea> และ <select> ทั้งหมดทำงานคล้าย ๆ กัน แท็กทั้งหมดนี้ยอมรับค่าที่เราใช้บน Component

 

 

ข้อมูลอ้างอิง : https://reactjs.org

กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
คำสั่งรวมไฟล์ และ บีบอัดในคำสั่งเดียว tar, zip
โดย mindphp พ 17 เม.ย. 2024 7:42 pm บอร์ด Linux - Web Server
0
15
พ 17 เม.ย. 2024 7:42 pm โดย mindphp View Topic คำสั่งรวมไฟล์ และ บีบอัดในคำสั่งเดียว  tar, zip
เช็คขนาดพื้นที่ฐานข้อมูล แต่ละก้อน แต่ละฐานข้อมูลว่าใช้พื้นที่ไปเท่าไหร่ ด้วย Comamnd Line
โดย mindphp จ 15 เม.ย. 2024 11:10 pm บอร์ด PostgreSQL
1
118
จ 15 เม.ย. 2024 11:14 pm โดย mindphp View Topic เช็คขนาดพื้นที่ฐานข้อมูล แต่ละก้อน แต่ละฐานข้อมูลว่าใช้พื้นที่ไปเท่าไหร่ ด้วย Comamnd Line
การติดตั้ง WSL เพื่อใช้งาน Linux Terminal บน Windows
โดย tsukasaz ศ 12 เม.ย. 2024 2:25 pm บอร์ด Share Knowledge
0
175
ศ 12 เม.ย. 2024 2:25 pm โดย tsukasaz View Topic การติดตั้ง WSL เพื่อใช้งาน Linux Terminal บน Windows
Super Сasual Dating - Real Women
โดย heroxbay ศ 12 เม.ย. 2024 8:55 am บอร์ด Microsoft Office Knowledge & line & Etc
0
128
ศ 12 เม.ย. 2024 8:55 am โดย heroxbay View Topic Super Сasual Dating - Real Women
Unsurpassed Сasual Dating - True Females
โดย pongsu1968 ศ 12 เม.ย. 2024 5:47 am บอร์ด Microsoft Office Knowledge & line & Etc
0
152
ศ 12 เม.ย. 2024 5:47 am โดย pongsu1968 View Topic Unsurpassed Сasual Dating - True Females
Question Tag ใช้อย่างไรในภาษาอังกฤษ
โดย internTk21 พฤ 11 เม.ย. 2024 10:46 pm บอร์ด Microsoft Office Knowledge & line & Etc
0
76
พฤ 11 เม.ย. 2024 10:46 pm โดย internTk21 View Topic Question Tag ใช้อย่างไรในภาษาอังกฤษ
มารู้จัก Clause in English กันเถอะ
โดย internTk21 พฤ 11 เม.ย. 2024 4:26 pm บอร์ด Microsoft Office Knowledge & line & Etc
0
97
พฤ 11 เม.ย. 2024 4:26 pm โดย internTk21 View Topic มารู้จัก Clause in English กันเถอะ
เรียนรู้การเปลี่ยน single noun เป็น plural noun
โดย internTk21 พฤ 11 เม.ย. 2024 3:29 pm บอร์ด Microsoft Office Knowledge & line & Etc
0
81
พฤ 11 เม.ย. 2024 3:29 pm โดย internTk21 View Topic เรียนรู้การเปลี่ยน single noun เป็น plural noun