reactjsnext.jsfacebook-social-plugins

How can use social share button for NEXTJS?


I want to use social share button for nextjs project so I am use this package https://www.npmjs.com/package/react-share but not work very well for exapmle share cout for facebook and linkedin and twitter nothing to show and I want to show total share count which does not have this option How can I use https://donreach.com/social-share-count/ for nextjs project?


Solution

  • If you can find an API that will give you social share counts from a given URL, you can make a request to it from a JS function, and get the data back. You can call this JS function from getInitialProps.

    See: "Data Fetching: getInitialProps | Next.js" https://nextjs.org/docs/api-reference/data-fetching/getInitialProps

    Taken from the docs:

    function Page({ stars }) { 
      return <div>Next stars: {stars}</div> 
    } 
    
    Page.getInitialProps = async (ctx) => { 
      const res = await fetch('https://api.github.com/repos/vercel/next.js');
      const json = await res.json();
      return { stars: json.stargazers_count }
    }
    
     export default Page