2017-10-12 95 views
0

代碼片段:通過React是否限制使用未聲明的變量?

enter image description here

class Box extends React.Component{ 
    render() { 
    params = new URLSearchParams(this.props.location.search); 
    abc = params.get('abc'); 
    console.log(params); 

錯誤 「創建應用程序做出反應」:

enter image description here

我爲什麼在這裏得到一個錯誤?

+2

這是create-react-app中的默認linting/strict-mode。但是你不應該把變量放在周圍,這是默認的原因。 –

回答

1

在非嚴格模式下,對未聲明符號的分配是 隱式地被視爲創建全局變量。在嚴格模式下, 這是一個錯誤。

我剛剛檢查了反應編譯後的代碼,它頂部有"use strict";。所以是的,React限制未聲明的變量。