2017-03-01 50 views
0

我正在使用React ES6应用程序。es6变成字符串

是否可以声明一个变量作为变量名,如下面的refInput一样?

renderInput(inputName) { 
    const refInput = `${inputName}Input`; 

    return (
     <Input 
      ref={(node) => { 
       this.refInput = node; 
      }} 
     /> 
    ); 
} 

任何帮助表示赞赏,我真的与此

+0

您可以动态构造对象属性名称,但不能变量名称。 – Pointy

+0

你在寻找'this [refInput] = node;'? –

回答

0

属性添加到对象动态挣扎,你可以在一个object.or可以ref element by attribute with string name使用属性托架访问符号[string_name]

renderInput(inputName) { 
    const refInput = `${inputName}Input`; 

    return (
    <Input 
     ref={(node) => { 
      this[refInput] = node; 
     }} 
    /> 
); 
} 
+0

FWIW,'[]'不是操作员。 –

+0

它标记为[MDN]中的运算符(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) –

+0

MDN不是真相的来源,规范是。 MDN总体来说很棒,但它有一些小的不准确之处。 –