动态 href 标签 React in JSX.
Dynamic href tag React in JSX
// This Javascript <a> tag generates correctly
React.createElement('a', {href:"mailto:"+this.props.email}, this.props.email)
但是,我正在努力在 JSX 中重新创建它
<a href="mailto: {this.props.email}">{this.props.email}</a>
// => <a href="mailto: {this.props.email}"></a>
href 标签认为{this.props.email}
是一个字符串,而不是动态输入 {this.props.email}
的值。关于我哪里出了问题的任何想法?
它返回一个字符串,因为您要将其分配给字符串。
您需要将其设置为动态属性,该属性的开头包含一个字符串
<a href={"mailto:" + this.props.email}>email</a>
一种稍微更ES6的方法来执行Patrick的建议,方法是使用模板文字:
<a href={`mailto:${this.props.email}`}>email</a>
在我看来,更好的方法是将其拆分为函数和JSX attr,如下所示:
<Button
onClick=sendMail
>
Send mail
</Button>
const sendMail = () => {
const mailto: string =
"mailto:mail@gmail.com?subject=Test subject&body=Body content";
window.location.href = mailto;
}
我认为这可能很容易...试试吧
<a href={`mailto:${email}?subject=${subject}&body=${text}`}>{email}</a>
相关文章:
- 如何在ReactJS JSX中执行嵌套的if-else语句
- Node.js's Buffer.writeFloatBE in Javascript
- Setting default onclick behavior for <img> tag in gene
- Lucene Search in Alfresco
- grep in JQuery to C#
- write HTML in JavaScript
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- HTMLInputElement in IE7
- Mongodb$in以与数组中相同的顺序获取结果
- element.dataset in Internet Explorer
- GoogleMap JSX组件中未定义的变量
- 如何在Yii2资产捆绑包中使用JSX文件
- 在<代码>使用JSX标记
- jQuery parent() in table
- Instagram oauth flow in angularjs
- 动态 href 标签 React in JSX.
- App.jsx:43 Uncaught (in promise) TypeError: this.state.conce
- JSX for...in loop