LoginSignup
2
4

More than 3 years have passed since last update.

ant designのFormを使ってみた

Last updated at Posted at 2020-04-16

Ant Designを使ってみて、Formコンポーネントで一瞬つまったのでめも

概要

  • AntDesignは、Reactのプロジェクトでデザインを楽にしてくれるやつ
  • Formの基本的な使い方
  • つまったところ & 解決法

Formの使い方

<Form>の中に<Form.Item>をネストして、<Form.Item>の中にInputとかSelectとかを入れていく

最低限のFormの例

// 省略
import { Form, Input, Button } from 'antd';

const FormComponent = () => {
  // フォーム送信の処理
  const onFinish = values => console.log(values)

  return (
    // 表示されるフォーム
    <Form onFinish={onFinish}>
      <Form.Item name="user" label="ユーザ名">
        <Input />
      </Form.Item>
      <Form.Item name="password" label="パスワード">
        <Input.Password />
      </Form.Item>
      <Form.Item name="password" label="パスワード">
        <Button htmlType="submit">
          送信
        </Button>
      </Form.Item>
    </Form>
  )
}

export default FormComponent

つまったとこ

onFinishを実行すると、consoleに{}が表示された i.e. valuesが空だった

→解決法: Form.Itemnameを設定する

最後のhtmlType="submitを持つ<Button>を押すと、onFinishが実行されるのですが、最初にFormを書いてたときに、Form.Itemのnameを書いてませんでした
これが原因で、onFinishの引数valuesに値が渡ってませんでした

nameを入れたら、onFinishの結果が

{ name: value }

にちゃんとなりました

2
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
2
4