reactjs - 在另一个 prop reactJS 中使用 prop 值
问题描述
我有一个带有消息字段的横幅组件 - 在本例中是欢迎消息。
其中一个道具称为'headline'
,您可以在其中输入您的消息。目前我正在使用消息“嗨乔,欢迎使用您的帐户”,但我想从另一个道具中提取名称。
这是我目前在 ReactDOM.render 中的内容:
ReactDOM.render(React.createElement(PriorityComponents.HeaderBanner, {
desktopImage:{},
mobImage:{},
altText:'alt test goes here',
isLeftAligned:true,
userName: 'Joe',
headline: 'Hi Joe, welcome to your account',
label: ''
}), document.getElementById("HeaderBanner"));
标题调用如下:
<Editable tag="h1" className="headerBanner__body__headline" html={article.headline} />
我想我也许可以使用以下内容:
userName: 'Joe',
headline: 'Hi ' + userName + ', welcome to your account'
这似乎破坏了组件!知道如何在标题道具中引用用户名吗?
解决方案
您应该能够像这样使用ES6 模板文字:
const username = 'Joe';
// While sending props
username: {username}
headline: {`Hi ${username}, welcome to your account`}
react 中对es6 对象简写的支持仍然是 wip,所以你还不能将它用于用户名。
推荐阅读
- ember.js - 迁移到组件的本机类时出现意外的令牌错误
- docker - Docker 运行到 Kubernetes 部署 yaml
- javascript - 如何比较来自更改事件的值
- ios - 滑动 ViewController 以关闭 - 顶部的错误
- asp.net - 为什么数据库没有变化
- dialogflow-es - 有没有办法动态激活对话流意图?
- django - Django:添加外键字段后迁移时出现 IntegrityError
- python - 如何处理 Python 日志中的常规消息?
- python - 将缺失的记录插入到由日期列表管理的对象数组中
- css - flexbox - 减少垂直间距