Home > Blockchain >  How to get state's value when the state’s key is a variable?
How to get state's value when the state’s key is a variable?

Time:02-01

is there a way to use a variable to get a value from state

I want to get a value from state, but the problem is that I’m receiving the state's key from parameter.

Is there any way to do it directly? (Similar to what is represented in image)

CodePudding user response:

You have to use the variable in brackets like shown below:

function SampleFunc(name){
  const ddlValue = this.state[name]
}

Also it’s better to use const instead of let when you know you don’t want to change the variable’s value in it’s scope.

  • Related