3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【React】ボタン押されて表示される編集フォームを作成する方法

Last updated at Posted at 2020-05-06

情報を探しても意外に出てこなかったので、Reactで以下のような編集フォームを作成するためのメモ。

Image from Gyazo

まず、create-react-appで雛形アプリを作成。

npx create-react-app edit-form
cd edit-form
yarn start
create-react-app.jpg

App.jsの中身は以下のように空っぽにしておく。

edit-pic1.png

これで作業の準備は整った。

それでは、本題の編集フォームを入れる<EditForm />というコンポーネントを作成してみる。

srcEditForm.js

import React, { Component } from "react"

export default class EditForm extends Component {
  constructor(props) {
    super(props)
    this.state = {
      input: "",
      editing: true,
    }
    this.handleEditing = this.handleEditing.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }
  render() {
    const { editing } = this.state
    return (
      <div>
        <h1>EditForm</h1>
        <form>
          {editing ? (
            <div>
              <input
                onChange={(e) => {
                  this.setState({ input: e.target.value })
                }}
                type="text"
                value={this.state.input}
              />

              <button onClick={this.handleSubmit}>Save</button>
            </div>
          ) : (
            <div>
              <span>{this.state.input}</span>
              <button onClick={this.handleEditing}>Edit</button>
            </div>
          )}
        </form>
      </div>
    )
  }

  handleEditing(e) {
    e.preventDefault()
    this.setState({
      editing: !this.state.editing,
    })
  }

  handleSubmit(e) {
    e.preventDefault()
    if (!this.state.input) return
    this.setState({
      input: this.state.input,
      editing: !this.state.editing,
    })
  }
}

あまり難しいことはしていない。

editingというstateを用意し、trueの時は文字列とEditボタンでfalseの時は入力フォームとSaveボタンが表示されるよう以下のような三項演算子を使用した。

{editing ? (
            <div>
              trueの時の表示
            </div>
          ) : (
            <div>
              falseの時の表示
            </div>
          )

そして、EditボタンやSaveボタンの押した時のonClickで以下のようにtrue/falseを切り替えている。

this.setState({ editing: !this.state.editing })

フォームの中身は、以下のようにしてonChangeが発火するたびに表示が切り替わるようになっている。

<input
  onChange={(e) => {
    this.setState({ input: e.target.value })
  }}
  type="text"
  value={this.state.input}
/>

あとはフォームのvalueとして値が変更されたinputというstateが、SaveボタンのonClickで実行されるhandleSubmit( )で更新されればOK。

handleSubmit(e) {
    e.preventDefault()
    if (!this.state.input) return
    this.setState({
      input: this.state.input, // ここでinputの値を更新して
      editing: !this.state.editing, // 同時にeditingをfalseに
    })
  }

作成した<EditForm />を忘れずに<App />でimportしておく。

srcApp.js

import React from "react"

import EditForm from "./EditForm"

function App() {
  return (
    <div className="App">
      <EditForm />
    </div>
  )
}

export default App

これで、Reactで文字列の編集ができるようになった。

Image from Gyazo

おわり。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?