Prop assigned value in constructor shows typeof undefined

Hi. My num1 prop returns undefined when checked with typeof (I expected ‘number’) however I did set a default value in the constructor (0). I’ve googled to no avail. Help plz, thanks!

Checking typeof inside setForNextNum(), line 83

CodePen: pen link

You are accessing a prop inside state object, so it should be,




Ah… thanks!

“State object” refers to the instance of MyApp? I thought I could access any prop within the class just with this (this.myProp). Would you mind elaborating a bit?

Yeah so you can access anything inside your constructor that has been declared with this.

If you had this.num1 in your constructor, your code would have worked, but since your num1 variable was inside

this.state = {
  num1: 0,

now you have to use this.state.num1 because state is an object and you are accessing a value inside the object.

Ah! I get it, thank you!