← All articles

Twitter Share Feature

Easily adding a simple share button to a blog around sharing on Twitter

Josh Hamilton4 min read
Editorial illustration of paper planes taking flight from an open magazine.
Reading appearance

Preferences stay in this browser.

Keep or share this article
Download Markdown

Use your browser’s print dialog to print or save a PDF.

You can also select and copy the citation above.

Look up a technical term →

There are so many tools out there that you can use to add a share button on your blog for social media. But, that also begs the question why not build your own, is it really that hard. In this post I will go over how simple it is to adding a tweet share button for twitter to your blog posts.

Code Snippet

We will begin with a simple code snippet that literally does the bare minimum and requires no tie-ins to any frameworks. The snippet is in React but you can easily see it uses no React internals to accomplish this.

import React from 'react'

export default function Share({ post }) {
  const url = window.location.href
  const message = `${url} ${post.excerpt}`

  return (
    <div>
      <a
        href={`https://twitter.com/intent/tweet?text=${encodeURIComponent(
          message
        )}`}
        target="_blank"
        rel="noreferrer"
      >
        Share on Twitter
      </a>
    </div>
  )
}

From the above you can see we are just creating a link and passing the context of the tweet to the text param passed to twitter. You can get fancy with this and add some logos with it but ultimately if you are reaching for a third party library to add this type of feature it’s no different than requiring in left-pad to pad a string.

Rebuilding the Wheel

There is an argument out their about not rebuilding the wheel and try to use solutions that are already out their. I say I agree with this most of the time but when you are requiring in something that basically just compiles a url to send to twitter and you have to override all of the styles to get it to look like how you would like then that is using a wheel that has been living in a junk yard and requires a lot of work to get back to a ready to use state, and not worth it.

Your private notes

Only on this device. Notes are never published or sent to a server. Keep up to 100 notes, with 10,000 characters per article. A full store never discards an older note.

Enable JavaScript to keep private notes.

Keep the curiosity going

pivot

The Pivot: Reusing a Stable SaaS Codebase for EasyAccessQR.com

How a stable SaaS foundation made it fast to launch EasyAccessQR.com, with analytics, A/B testing, and a full design-flow rewrite.

6 min read
ai

Building AGFS.dev on my wife's MacBook Neo with an AI agent

Why the citrus MacBook Neo is more capable than it looks, and why AI-assisted coding makes it a legit first developer machine.

6 min read
ai

Building AGFS.dev to make remote agent files easy to share

Why I built AGFS.dev for remote agent workflows, why Git is the wrong tool for artifacts, and how I put the web app plus CLI together.

7 min read

Article image

Open original image ↗

Keyboard shortcuts

Shortcuts stay out of text fields. Extra shortcuts are off until you enable them on this device. Ctrl or Command K is always available.

?
Open this help
Ctrl / ⌘ K
Open site search
Alt A
Browse articles
Alt R
Open your reading list
Alt T
Browse topics
Escape
Close dialogs or leave article focus mode