1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

react-hook-formでデータが取得できない解決方法について

Posted at

はじめに

react-hook-formで入力したデータをコンソールで表示できなかったため、そちらの解決方法を共有します。


下記のように記述していました。

.Register.tsx
import { Box, Card, CardBody, CardHeader, FormLabel, Heading, Input, Radio, RadioGroup, Stack, Textarea } from "@chakra-ui/react"
import { useForm } from "react-hook-form";

export const Register = () => {
    const { handleSubmit } = useForm();

    return (
        <div>
            <Card>
                <CardHeader>
                    <Heading size='md'>新規名刺登録</Heading>
                </CardHeader>
                <CardBody>
                    <Box>
                    <form onSubmit={handleSubmit((data) => {
                        console.log("dataの中身",data);
                    })}>
                        <FormLabel>ユーザーID</FormLabel>
                        <Input type="text" placeholder="ユーザーIDを入力してください" />
                        <FormLabel>名前</FormLabel>
                        <Input type="text" placeholder="名前を入力してください" />
                        <FormLabel>自己紹介</FormLabel>
                        <Textarea placeholder="自己紹介文を入力してください。HTMLタグも使えます" />
                        <FormLabel>好きな技術</FormLabel>
                        <RadioGroup >
                            <Stack direction='row'>
                                <Radio value='React'>React</Radio>
                                <Radio value='TypeScript'>TypeScript</Radio>
                                <Radio value='Github'>Github</Radio>
                            </Stack>
                        </RadioGroup>
                        <FormLabel>GitHub ID</FormLabel>
                        <Input placeholder="IDを入力してください" />
                        <FormLabel>Qiita ID</FormLabel>
                        <Input placeholder="IDを入力してください" />
                        <FormLabel>X ID</FormLabel>
                        <Input placeholder="IDを入力してください" />
                        <Input type="submit" />
                    </form>
                    </Box>
                </CardBody>
            </Card>


        </div >
    )

}

解決方法

registerを使う

.Register.tsx
import { Box, Card, CardBody, CardHeader, FormLabel, Heading, Input, Radio, RadioGroup, Stack, Textarea } from "@chakra-ui/react"
import { useForm } from "react-hook-form";

export const Register = () => {
    const { register, handleSubmit } = useForm();

    return (
        <div>
            <Card>
                <CardHeader>
                    <Heading size='md'>新規名刺登録</Heading>
                </CardHeader>
                <CardBody>
                    <Box>
                    <form onSubmit={handleSubmit((data) => {
                        console.log("dataの中身",data);
                    })}>
                        <FormLabel>ユーザーID</FormLabel>
                          <Input type="text" placeholder="ユーザーIDを入力してください" {...register("user_id") }/>
                        <FormLabel>名前</FormLabel>
                          <Input type="text" placeholder="名前を入力してください" {...register("name")}/>
                        <FormLabel>自己紹介</FormLabel>
                          <Textarea placeholder="自己紹介文を入力してください。HTMLタグも使えます" {...register("dedcription")}/>
                        <FormLabel>好きな技術</FormLabel>
                          <RadioGroup>
                            <Stack direction='row'>
                              <Radio value='React'>React</Radio>
                              <Radio value='TypeScript'>TypeScript</Radio>
                              <Radio value='Github'>Github</Radio>
                            </Stack>
                          </RadioGroup>
                        <FormLabel>GitHub ID</FormLabel>
                          <Input placeholder="IDを入力してください" {...register("github_id")} />
                        <FormLabel>Qiita ID</FormLabel>
                          <Input placeholder="IDを入力してください" {...register("qiita_id")} />
                        <FormLabel>X ID</FormLabel>
                          <Input placeholder="IDを入力してください" {...register("x_id")} />
                        <Input type="submit" />
                    </form>
                    </Box>
                </CardBody>
            </Card>


        </div >
    )

}

参考

おわりに

registerの意味を理解することができました。


1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?