Multi step form or conditional rendering with DevTool console get an error Cannot update a component (`FormItemTest`) while rendering a different component (`FormItemTest`). To locate the bad setState() call inside `FormItemTest`

オープン
#226 コメント 0 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
react, typescript
領域
devtools

調査の方向性

Start with the provided Demo reproduction using FormProvider, conditional Step2 rendering, getStep(step), and . Reproduce the warning with DevTool enabled, then inspect the DevTool component and its control subscription or rendering path. Done means the multi-step example no longer reports the setState-during-render warning while DevTool remains enabled.

索引モデルが issue の本文から書いたものです。

説明

The collection of fields in react hook form meets expectations. Confirmed to be a bug in DevTool. when I annotate DevTool,no more errors reported.

this is example code

import {
  useForm,
  FormProvider
} from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { DevTool } from '@hookform/devtools';

import FormItemTest from '~/components/form/FormItemTest';

import {
  useState
} from 'react';

import * as z from 'zod';

const schema = z.object({
  step1: z.string(),
  step2: z.string()
});

function Step1() {
  return (
    <FormItemTest name='step1' />
  )
}

function Step2() {
  return (
    <FormItemTest name='step2' />
  )
}

function getStep(step: number) {
  switch (step) {
    case 0:
      return <Step1 />;
    case 1:
      return <Step2 />;
    default:
      return null;
  }
}

export default function Demo() {
  const [show, setShow] = useState(false);
  const [step, setStep] = useState(0);

  const methods = useForm({
    resolver: zodResolver(schema),
    mode: 'onChange'
  });

  const onSubmit = methods.handleSubmit(values => {
    console.log(values);
  });

  return (
    <div>
      <FormProvider {...methods}>
        <form>
          <Step1 />
          {show && <Step2 />}
          {getStep(step)}
        </form>
      </FormProvider>
      <DevTool control={methods.control} />
      <button onClick={() => setShow(s => !s)}>Toggle Step2</button>
      <button onClick={onSubmit}>Submit</button>
      <button onClick={() => setStep(s => s + 1)}>Next Step</button>
    </div>
  )
}
Image
主要言語
TypeScript
スター
670
フォーク
55
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

react-hook-form/devtools のほかの issue

react-hook-form/devtools の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。