Renders a link formatted to send an email.
- Destructure the component’s props, use
email
,subject
andbody
to create a<a>
element with an appropriatehref
attribute. - Render the link with
props.children
as its content.
代码实现
function Mailto({ email, subject, body, ...props }) {
return (
<a href={`mailto:${email}?subject=${encodeURIComponent(subject) || ''}&body=${encodeURIComponent(body) || ''}`}>{props.children}</a>
);
}
使用样例
ReactDOM.render(
<Mailto email="[email protected]" subject="Hello & Welcome" body="Hello world!">
Mail me!
</Mailto>,
document.getElementById('root')
);