3

I have written a react component with a constructor, methods, and render. When I comment out the methods and have only the render and constructor everything is fine, but when I add the methods the first always comes up as undefined unless I close off the whole class after the constructor.

Here is my class (since it doesn't matter what is in the methods I just put the declarations for brevity):

export default class App extends Component {

  constructor(props) {
    super(props);
    const allWords = ["word", "pipe", "dear", "deer", "fish", "coat"];
    const word= allWords[Math.floor(Math.random()*allWords.length)];
    var lettersFound = [];
    for (var i=0; i<word.length;i++) {
      lettersFound = [...lettersFound, [i, "_"]];
    }
    this.fillWord = this.fillWord.bind(this);

    this.state = {
      word: word,
      numWrongGuesses: 0,
      lettersGuessed: [],
      lettersFound: lettersFound,
      error: ""
    }
  }

  isLetterInWord = (letter) => {
  }

  fillWord = () => {
  }



  handleGuess = (letter) => {
  }

  render () {
    return (
      <div className="App">
      </div>
    );
  }

}

This doesn't compile, giving the error "'isLetterInWord' is not defined". If I remove this function, the error is thrown on fillWord instead. If I add a "}" after the constructor the methods are all defined but render throws an unexpected token error on the return line. I do not understand why I would want to close off the class after the constructor, or why it isn't compiling as is.

5
  • how do you call the isLetterInWord method ? Commented May 4, 2019 at 20:30
  • Running your code in this sandbox: codesandbox.io/s/zxpnk301pl. It doesnt seem to be giving any errors. Commented May 4, 2019 at 20:32
  • 2
    maybe this plugin babeljs.io/docs/en/babel-plugin-proposal-class-properties in missing in his babel config Commented May 4, 2019 at 20:39
  • @OlivierBoissé I call the method in another method just as isLetterInWord(letter); That plug-in didn't seem to help. It's strange, before I added the isLetterInWord() the other functions worked just fine. Now I have this issue. Commented May 5, 2019 at 23:49
  • @ChristopherNgo it worked just fine until I added isLetterInWord in my browsers. It's very strange. Commented May 5, 2019 at 23:56

2 Answers 2

5

Try to bind the function inside the constructor like this:

this.isLetterInWord = this.isLetterInWord.bind(this);
this.fillWord = this.fillWord.bind(this)
this.handleGuess = this.handleGuess.bind(this);
1
  • 1
    I had deleted the "this." before my function call. Once I added that and the binding it worked. Thanks! Commented May 6, 2019 at 0:31
2

Here you are defining class properties, which is currently (I think) is stage-3 proposal. In other words it is not supported by default. May be because of this you are getting errors. You can use class-properties-transform plugin for Babel to add support of this syntax though if you want to .

isLetterInWord = (letter) => {
  }

  fillWord = () => {

  }

Try defining methods as and see if it works

isLetterInWord(letter) {

}
1
  • It looked like this was going to work because sublime text now highlights everything correctly, but it throws the same error in chrome and firefox still. Commented May 5, 2019 at 23:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.