react-shareでSNSシェアボタンを作る

公開日時

Next.jsアプリにSNSシェアボタンを追加する際に、react-shareを使うと手軽に導入できた。

↓のようなShareボタン用のコンポーネントを作り、呼び出し元からシェア文言とURLを渡すようにすればOK.

// components/Share.tsx
import React from 'react'
import { FacebookIcon, FacebookShareButton, TwitterIcon, TwitterShareButton } from 'react-share'

type Props = {
  text: string
  url: string
}

export const Share = ({ text, url }: Props) => {
  return (
    <ul className="flex justify-center list-none">
      <li className="mr-8">
        <TwitterShareButton url={url} title={text}>
          <TwitterIcon size={32} round={true} />
        </TwitterShareButton>
      </li>
      <li>
        <FacebookShareButton url={url}>
          <FacebookIcon size={32} round={true} />
        </FacebookShareButton>
      </li>
    </ul>
  )
}

今回はTwitter, Facebookのサンプルにしたが、react-shareはLINEやHatenaブックマークなど様々なSNSに対応している。


Related #next.js

SharedArrayBuffer updates in Android Chrome 88 and Desktop Chrome 92

クロスオリジン分離対応を実施

react-hook-formとReact Datepickerを組み合わせる

Hook FormのControllerを使う

Next.jsで生成したサイトで特定のページのみnoindexを設定する

タグに紐づく記事一覧ページはnoindexにした

Next.jsでAdsenseタグを埋め込んだら Only one AdSense head tag supported per page エラーが発生

Only one AdSense head tag supported per page. The second tag is ignored.