DEV Community

hkievet
hkievet

Posted on

8 2

An easy way to hide an email from bots with react.

An easy way to hide an email from bots with react.

I think that it's important to protect your email address from webcrawlers.

Some companies use contact us forms, some freelance developers put the email address in the form of "contact me at foobar @ gmail dot com" or something like that.

This is a fun way to retrieve an email address from a static react site by leveraging lazy loading. It's a pretty simple concept: lazy load a component containing the email address based on a trigger (in this case a button click).

The code:

import * as React from "react";

export interface IContactMeHrefProps {}

export const ContactMeHref: React.FC<IContactMeHrefProps> = props => {
  return (
    <div>
      <a href="mailto:foobary@gmail.com">
        foobar@gmail.com
      </a>
    </div>
  );
};

export default ContactMeHref;
import * as React from "react";
const ContactMeHref = React.lazy(() => import("../resume/Contact"));

export interface IContactMeGateProps {}

// Make user click a button to show email adderss via lazy loading
export const ContactMeGate: React.FC<IContactMeGateProps> = props => {
  const [showingEmail, setShowingEmail] = React.useState(false);

  let email = showingEmail ? (
    <ContactMeHref />
  ) : (
    <button
      onClick={() => setShowingEmail(true)}
    >
      Click for contact info
    </button>
  );
  return (
    <React.Suspense fallback={<div>loading...</div>}>
      <div>
        {email}
      </div>
    </React.Suspense>
  );
};

export default ContactMeGate;

You can find an example of it on my website at hkievet.com.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (3)

Collapse
 
tobiassn profile image
Tobias SN

I would recommend hiding it behind a CAPTCHA just to be safe.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
azivkovi profile image
Ante

This is a clever solution - thank you. Introducing captcha for a tiny use case like this might be overkill.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️