如何在React上获得动态参考

时间:2015-11-17 19:03:18

标签: javascript reactjs

我有一个带有这个参考的元素

const chipBetImage = this.refs.inner-player${this.props.position};

并且在一个函数中我需要使用那个ref做这样的事情

./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');

但是我收到了错误

{{1}}

那么,这是做这个的方法吗?

1 个答案:

答案 0 :(得分:2)

images仅在模板文字内有效。 ${}不是模板文字。

如果要使用计算属性名称,则必须使用括号表示法:

this.refs.inner-player${this.props.position};

请参阅Dynamically access object property using variable

相关问题