1012 B
1012 B
title, tags, expertise, author, cover, firstSeen, lastUpdated
| title | tags | expertise | author | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|---|
| Email link | components | beginner | chalarangelo | blog_images/digital-nomad-4.jpg | 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,subjectandbodyprops to create a<a>element with an appropriatehrefattribute. - Use
encodeURIcomponentto safely encode thesubjectandbodyinto the link URL. - Render the link with
childrenas 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')
);