Search code examples
reactjscreate-react-app

does React restrict usage of undeclared variables?


Code snippet:

enter image description here

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

Error via "Create React App":

enter image description here

Why am I getting an error here?


Solution

  • In non-strict mode, an assignment to an undeclared symbol is implicitly treated as creating a global variable. In strict mode, that's an error.

    I just checked Reacts compiled code and it has "use strict"; at the top. So yes, React restricts undeclared variables.