0

I recently started to learn React Native and ran into this something which I am not quite sure about. I have multiple ways to solve the problem, but I believe if there is a solution to what I am asking, it may be helpful in the future.

The following is snippet of my code:

this.State = { 
    bedTime: 25, 
    breakfastTime: 5,
} 

timer = timeType => {
      this.setState({
        timeType: this.state.timeType -1,
      })
  }

someFunction() {
    this.timer(bedTime)
}

Now I could pass the parameter as string (ex. "bedTime") but then I'd have to make conditional statement in the function I am calling/passing. Can this problem be solved in the way I asked?

Thank you in advance :)

Danish Saeed
  • 49
  • 1
  • 5
  • this should be `this.timer(this.state.bedTime)` – Sifat Haque Apr 30 '20 at 10:13
  • If you are trying to pass the variable name then you need to pass like this `this.timer("bedTime")` – Sifat Haque Apr 30 '20 at 10:15
  • yes, I actually did it like you mentioned this.timer("bedTime"), but that requires me to use a conditional statement in the function I am calling. So I guess passing the variable name directly is not possible. Thank you for your response tho – Danish Saeed Apr 30 '20 at 10:46

0 Answers0