LoginSignup
6
1

More than 3 years have passed since last update.

react-bootstrapでWarning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>

Last updated at Posted at 2020-03-26

react-bootstrapでこのエラーに遭遇して対処法を調べたのですが日本語の記事が少なくて解決に時間がかかったのでメモとして残しておきます。

エラーの原因

コンソールには以下のエラーが出力されます

Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>.

原因がわからずググってみたら、このサイトによると<div>タグを<p>タグでラップしているとこのエラーが起こるようです。
しかし、<p>タグを使っている箇所がない、、、

解決策

原因となっていたのは以下のコードでした


              <Card.Text>
              <Row style={{ clear: "both" }}>
                <SelectButton 
                  isSelected={selectedPictureList.includes(picture)}
                  target={picture}
                  selectedFunc={selectPicture}
                  unselectedFunc={unselectPicture}
                />
                <PicturesCommentNewContainer pictureId={picture.id} />
              </Row>

              </Card.Text>

こちらのサイトによると、<Card.text>の出力は<p>タグになってしまうそうです。
その中のコンポーネント(PicturesCommentNewContainer)でdivタグを返す処理をしていたので、エラーが起こっていたみたいですね...


              <Row style={{ clear: "both" }}>
                <SelectButton 
                  isSelected={selectedPictureList.includes(picture)}
                  target={picture}
                  selectedFunc={selectPicture}
                  unselectedFunc={unselectPicture}
                />
                <PicturesCommentNewContainer pictureId={picture.id} />
              </Row>

このように、単純に<Card.Text>を使わなければエラーは無事なくなりました
bootstrapめんどい!

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