如何在React上获得动态引用
How to get dynamic ref on React
我有一个带有此ref 的元素
ref={`inner-player${this.props.position}`}
在一个函数中,我需要和那个裁判一起做一些类似的事情
const chipBetImage = this.refs.inner-player${this.props.position};
但是我收到一个错误
./app/components/ui/PlayerSlot/index.js
Module build failed: SyntaxError: /home/marcelo/Documents/Projects/application-playerinterface/app/components/ui/PlayerSlot/index.js: Unexpected token (150:50)
148 | this.props.addMoney({position : this.props.position, currentBet : this.props.currentBet});
149 | } else {
> 150 | const chipBetImage = this.refs.inner-player${this.props.position};
| ^
151 | chipBetImage.classList.add('animated', 'pulse');
那么,该怎么做呢?
${}
仅在模板文本内有效。this.refs.inner-player${this.props.position};
不是模板文字。
如果你想使用计算的属性名称,你必须使用括号表示法:
this.refs[`inner-player${this.props.position}`]
请参阅使用变量动态访问对象属性。
相关文章:
- 在动态创建的元素上获取对特定选择器的引用
- 通过动态创建数组名称在单击函数中引用 javascript 数组
- 使用 Reactjs 保留对动态子引用的引用
- 存储对追加动态添加的元素的引用
- '这'引用了jQuery on()中错误的动态生成元素
- 如何动态引用 jQuery 插件实例
- 在 javascript 中使用变量来动态引用 html 元素
- JQuery:如何动态引用元素
- 动态引用函数时的Javascript变量范围
- 如何在React上获得动态引用
- 动态引用导入
- 使用参数动态引用json对象
- 动态引用变量(对象)
- 动态引用类属性
- 在Yii中使用javascript动态引用对象ID
- 动态引用JQUERY中的子元素
- 如何在Javascript中动态引用字符串
- 在开头或动态引用javascript
- 动态引用一个嵌套的Javascript对象
- 如何动态引用对象