【发布时间】:2020-07-31 01:01:24
【问题描述】:
我是 JSX 和一般 javascript 的新手,我对 {} 根据其内部和周围的内容具有多种含义感到困惑。在React docs 的以下示例中,我对{} 和${} 之间的区别感到困惑。
在一个解释钩子的例子中,我们看到<button onClick={() => this.setState({ count: this.state.count + 1 })}>
这里,最里面的花括号表示带有一对key: value 的JS 对象是作为唯一参数传递给this.setState() 的对象。
后来,在componentDidUpdate 和componentDidMount 方法中我们看到document.title = `You clicked ${this.state.count} times/`;
在这个 sn-p 中,花括号不包围 key: value 对,所以我知道它不是 JSON 对象。它在 JSX 中的 {} 内,所以 this.state.count 应该只计算为一个数字,但这个表达式中 $ 的含义是什么?
提前致谢!
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
componentDidMount() {
document.title = `You clicked ${this.state.count} times`;
}
componentDidUpdate() {
document.title = `You clicked ${this.state.count} times`;
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}
【问题讨论】:
标签: javascript jsx