1

class Question extends React.Component {
  constructor(props) {
    super(props);
    this.randInt = Math.floor(Math.random() * 1);
  }

  question = {
    quest: questions[this.props.randInt].quest,
    answers: questions[this.props.randInt].answers,
    correct: questions[this.props.randInt].correct
  }
}

I'm trying to get value of randInt but I get an Error

TypeError: Cannot read property 'quest' of undefined

9
  • What is questions ? Also, what are you trying to use the question Object for? When do you want to initialize it? Commented Mar 9, 2019 at 18:26
  • That's an object imported from external js file Commented Mar 9, 2019 at 18:27
  • instead of this.props.randInt do this.randInt Commented Mar 9, 2019 at 18:28
  • It still doesn't work and throws the same error Commented Mar 9, 2019 at 18:29
  • 1
    Also, Math.floor(Math.random() * 1) returns always 0. Commented Mar 9, 2019 at 18:29

3 Answers 3

2

Since it's this.randInt and not this.props.randInt, it should be this.randInt everywhere.

question is a class field, which is syntactic sugar for constructor code:

class Question extends React.Component{
    constructor(props){
      super(props);

      this.question = {
        quest : questions[this.props.randInt].quest,
        answers : questions[this.props.randInt].answers,
        correct : questions[this.props.randInt].correct
      }

      this.randInt = Math.floor(Math.random() * 1);
    }
}

Class field declarations go first in the constructor body. Since an explicit constructor is not needed here, it should be:

class Question extends React.Component{
      randInt = Math.floor(Math.random() * 1);

      question = {
        quest : questions[this.randInt].quest,
        answers : questions[this.randInt].answers,
        correct : questions[this.randInt].correct
      } 
}

With class fields, explicit constructor is needed only for synchronous side effects that previously occurred in deprecated componenentWillMount lifecycle method.

Sign up to request clarification or add additional context in comments.

Comments

1

you need this.question instead of question

Comments

0

Use this.randInt instead. randInt is not a prop, rather defined on the class.

class Question extends React.Component {
  constructor(props) {
    super(props);
    this.randInt = String(Math.floor(Math.random()*2) + 1);
  }

  question = {
    quest: questions[this.randInt].quest,
    answers: questions[this.randInt].answers,
    correct: questions[this.randInt].correct
  }
}

5 Comments

can you provide the questions Object. Also Math.floor(Math.random() * 1) returns 0
questions={1:{ quest: "fasfs", answers: [1812, 1837, 1864, 1899], correct: 1837 }, 2:{ quest: "fasfag", answers": [2,3,4,5], correct: 4 }}
there's no 0 key on your questions object. Obviously it's going to error out.
Okay, i tried Math.floor(Math.random()*2) + 1 but same
convert it to string, since you're accessing the property on an object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.