React是否限制使用未声明的变量?
问题描述:
代码片段:通过React是否限制使用未声明的变量?
class Box extends React.Component{
render() {
params = new URLSearchParams(this.props.location.search);
abc = params.get('abc');
console.log(params);
错误 “创建应用程序做出反应”:
我为什么在这里得到一个错误?
答
在非严格模式下,对未声明符号的分配是 隐式地被视为创建全局变量。在严格模式下, 这是一个错误。
我刚刚检查了反应编译后的代码,它顶部有"use strict";
。所以是的,React限制未声明的变量。
这是create-react-app中的默认linting/strict-mode。但是你不应该把变量放在周围,这是默认的原因。 –