0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

styled-components: it looks like an unknown prop "isVisible" is being sent through to the DOM, which will likely trigger a React console error. If you would like automatic filtering of unknown props, you can opt-into that behavior via `<StyleSheetManager shouldForwardProp={...}>` (connect an API like `@emotion/is-prop-valid`) or consider using transient props (`$` prefix for automatic filtering.)

styled-componentsを使用して作成したコンポーネントにisVisibleというpropを渡した際に上記の警告が出てきました。

コード

Img.tsx
import styled from "styled-components"

const Img = styled.img<{isVisible: boolean}>`
    {(isVisible) =>
        !isVisible &&
        css`
          visibility: hidden;
        `}
`
page.tsx
const Page = () => {
    return(
        <div>
            <Img isVisible={variable} />
        </div>
    )
}

原因

警告メッセージにもある通り、propがDOMに渡されていることで引き起こされているようです。
警告メッセージ末尾にあるようにpropに$マークをつけてあげましょう。

import styled from "styled-components"

+ const Img = styled.img<{$isVisible: boolean}>`
+     ${( isVisible ) =>
+        !$isVisible &&
        css`
          visibility: hidden;
        `}
`
page.tsx
const Page = () => {
    return(
        <div>
+            <Img $isVisible={variable} />
        </div>
    )
}

余談

警告メッセージの中には<StyleSheetManager shouldForwardProp={...}>を使うことも示唆されていますが、今後こちらの使用方法を調べたいと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?