0

I've got a strange one, this will probably be really easy for you guys... I'm building a method within my component to generate HTML for an input.

So in my render I'm using {this.generateInput('q1')}

Below is my method, so I want question which is 'q1' to replace 'question' in the const and console.log below...

generateInput( question ) {
   if (this.props.next) {
     const label = this.props.question;
     console.log( this.props.question.props.details.form_label );
   }
 }
  • You mean you want to access `this.props.q1`, when `"q1"` is passed to the function? – Tom Fenech Jan 27 '17 at 10:54
  • If I understand you correctly, you just need to use bracket notation for property access instead of dot notation. `this.props[question]` would be the same as `this.props.q1` if `question` was equal to `"q1"`. You can read more about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors). – Saad Jan 27 '17 at 10:55
  • Thanks @TomFenech and saadq, that's worked a charm. I just needed to do the following... `const label = this.props[question];` – Michael Weaver Jan 27 '17 at 11:11

0 Answers0