Can't find variable: counter, in expo

Been using React for a bit and wanted to try out native using expo, when I click on a button to increment a counter, I get a ReferenceError on my phone saying "Can't find variable: counter", I don't get any error on the expo gui or in VSCode, very confused.

import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { Button } from "react-native-elements";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: 0
    };
  }

  increment() {
    this.setState({ counter: (counter += 1) });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>{this.state.counter}</Text>
        <Button title="Press Me" onPress={this.increment}></Button>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
});


Change your increment function to

increment = () => {
    this.setState({
        counter: this.state.counter + 1
    });
}

Make sure to define your increment function as an arrow function otherwise you can't access it from Button.

<Button title="Press Me" onPress={this.increment}></Button>

Feel free for doubts.

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.


The counter variable you are referring inside increment function doesn't refer to the state variable. Access the state object using this.state.counter and then increment it by 1.

Please update the increment function as below

  increment() {
    this.setState({ counter: this.state.counter+ 1) });
  }

  <Button title="Press Me" onPress={() => this.increment()}></Button>

The Official CAN / Spoon Records Website, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can � Drink can � Oil can � Steel and tin cans � Trash can� Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the


So there are 2 ways of doing it, either you change your increment function to a fat arrow function or bind the this scope of function in constructor, Its basically how this scope gets binded:

increment = () => {
    this.setState({ counter: (counter += 1) });
  }

another way is :

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: 0
    };
this.increment = this.increment.bind(this); // new addition
  }

  increment() {
    this.setState({ counter: (counter += 1) });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Text>{this.state.counter}</Text>
        <Button title="Press Me" onPress={this.increment}></Button>
      </View>
    );
  }
}

Hope it gets clear, these are the 2 ways. Feel free for doubts

Can, 1Be able to. 'they can run fast'. More example sentences. 'I could hear footsteps� can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.


Can (band), can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.


Can, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). HANGZHOU, China, Aug. 31, 2020 (GLOBE NEWSWIRE) -- Canaan Inc. (NASDAQ: CAN) ("Canaan" or the "Company"), a leading high-performance computing solutions provider, today announced its unaudited


CAN, A can is a metal container in which something such as food, drink, or paint is put. The container is usually sealed to keep the contents fresh. Several young men� "Can" is one of the most commonly used modal verbs in English. It can be used to express ability or opportunity, to request or offer permission, and to show possibility or impossibility.