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?

More than 1 year has passed since last update.

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?の解決方法

Posted at

結論

子コンポーネントがaタグをラップしていないのにpassHrefを使用したことによるエラーだった模様

実際のエラー文

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

該当のコード

import { Phrases } from '../../types/types'
import PhraseCard from '../molecules/PhraseCard'
import CardCase from '../atoms/CardCase'
import Link from 'next/link'

const PhraseCardList: React.VFC<Phrases> = ({ phrases }) => {
  return (
    <ul className="w-full">
      {phrases &&
        phrases.map((phrase) => (
          <li key={phrase.id}>
            <Link href={`/phrases/${phrase.id}`} passHref>
              <CardCase hover={true}>
                <PhraseCard phrase={phrase} />
              </CardCase>
            </Link>
          </li>
        ))}
    </ul>
  )
}

export default PhraseCardList

修正ver

passHrefを使わないようにするとエラーは出なくなった

import { Phrases } from '../../types/types'
import PhraseCard from '../molecules/PhraseCard'
import CardCase from '../atoms/CardCase'
import Link from 'next/link'

const PhraseCardList: React.VFC<Phrases> = ({ phrases }) => {
  return (
    <ul className="w-full">
      {phrases &&
        phrases.map((phrase) => (
          <li key={phrase.id}>
            <Link href={`/phrases/${phrase.id}`}>
              <a>
                <CardCase hover={true}>
                  <PhraseCard phrase={phrase} />
                </CardCase>
              </a>
            </Link>
          </li>
        ))}
    </ul>
  )
}

export default PhraseCardList

参考文献

同じことが書かれていた。

最後に

間違っていることがあれば指摘をお願いいたします。

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?