title: React setState for the const declared outside constructor
body: <p>I am new to React. I created a class component and there is a constructor. </p>
  
  <p>I created this.state inside the constructor function. </p>
  
  <p>On <code>componentDidMount</code> I called few functions and in those function create the const. </p>
  
  <p>Can I use <code>this.setState({})</code> for those variables declared outside the constructor and created and called in the <code>componentDidMount()</code> function? </p>
  
  <p>I got an error when I tried to use such variables in another function.</p>
  
  <p>My code is the following:</p>
  
  <pre><code>class App extends Component {
     constructor(props) {
       super(props);
       this.state = {
         web3: null,
         network: "",
         account: 0x0
    };
  }
  
   componentDidMount = async () =&gt; {
    await this.loadWeb3();
  };
  
   loadWeb3 = async () =&gt; {
    const web3 = await getWeb3();
    const accounts = await web3.eth.getAccounts();
    this.setState({ account: accounts[0] });
   const networkId = await web3.eth.net.getId();
  this.setState({ networkId });
  this.setState({ web3 });
  console.log(web3); //
  console.log(networkId); //got sucessfully console log over here
  await this.setNetwork();
  };
  
  setNetwork = async () =&gt; {
    let networkName,
     that = this;
  
    await this.state.web3.eth.net.getNetworkType(function(err, network) {
      switch (network) {
      case "main":
        networkName = "Main";
        break;
      case "morden":
        networkName = "Morden";
        break;
      case "ropsten":
        networkName = "Ropsten";
        break;
      case "private":
        networkName = "Ganache";
        break;
      default:
        networkName = this.state.networkId; //but got error over here "Unhandled Rejection (TypeError): Cannot read property 'state' of undefined"
    }
  
    that.setState({
      network: networkName
    });
  
    console.log(this.state.network);
  });
  };
  </code></pre>
  
  <p>I got following error in setNetwork() default switch case</p>
  
  <p>Unhandled Rejection (TypeError): Cannot read property 'state' of undefined</p>