2017-03-01 51 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總體來說很棒,但它有一些小的不準確之處。 –