Files
30-seconds-of-code/snippets/Mailto.md
Isabelle Viktoria Maciohsek 581bb672b1 Make expertise a field
2022-03-01 20:24:15 +02:00

952 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Email link components beginner 2019-01-28T20:10:51+02:00 2020-11-16T15:17:26+02:00

Renders a link formatted to send an email (mailto: link).

  • Use the email, subject and body props to create a <a> element with an appropriate href attribute.
  • Use encodeURIcomponent to safely encode the subject and body into the link URL.
  • Render the link with children as its content.
const Mailto = ({ email, subject = '', body = '', children }) => {
  let params = subject || body ? '?' : '';
  if (subject) params += `subject=${encodeURIComponent(subject)}`;
  if (body) params += `${subject ? '&' : ''}body=${encodeURIComponent(body)}`;

  return <a href={`mailto:${email}${params}`}>{children}</a>;
};
ReactDOM.render(
  <Mailto email="foo@bar.baz" subject="Hello & Welcome" body="Hello world!">
    Mail me!
  </Mailto>,
  document.getElementById('root')
);