LoginSignup
0
0

More than 1 year has passed since last update.

【React】Warning: validateDOMNesting(...): <h2> cannot appear as a child of <h2>.の対処法【JavaScript】

Last updated at Posted at 2021-07-08

症状

Reactでstyled-componentを使用した際に、以下、のWranが出てしまいました。
翻訳すると、「警告:validateDOMNesting(...):< h2 >は< h2 >の子として表示できません」と翻訳されました

Warning: validateDOMNesting(...): <h2> cannot appear as a child of <h2>.

該当のソースは以下です。

Hoge.jsx
import React from "react";
import styled from "styled-components";

const HogeWrapper = styled.h2`
`;

const HogeNameWrapper= styled.h2`
`;

ewport const = () => {
  const hogeName = "HogeName";

  return (
    <HogeWrapper>
      <HogeNameWrapper>{hogeName}</HogeNameWrapper>
    </HogeWrapper>
  )
}

解決方法

h2の外側のh2をdivにしたら、解決しました。
h2の要素な中に、h2要素を入れてしまったため、このようなエラーが出てしまったようです。

通常のタグの使用方法と同様で、styled-componentsでも文字を装飾するようなタグの中にタグを入れるのは推奨されていないようです。

Hoge.jsx
import React from "react";
import styled from "styled-components";

const HogeWrapper = styled.div`
`;

const HogeNameWrapper= styled.h2`
`;

ewport const = () => {
  const hogeName = "HogeName";

  return (
    <HogeWrapper>
      <HogeNameWrapper>{hogeName}</HogeNameWrapper>
    </HogeWrapper>
  )
}
0
0
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
0
0