0

I am working on a React ES6 application.

Is it possible to declare a variable which will be taken as a variable name, like below with refInput ?

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

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

Any help is appreciated, I am really struggling with this

Franck
  • 79
  • 7

1 Answers1

0

add properties into an object dynamically,you can use property bracket accessor notation [string_name] on an object.or you can ref element by attribute with string name.

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

  return (
    <Input
        ref={(node) => {
            this[refInput] = node;
        }}
    />
  );
}
holi-java
  • 29,655
  • 7
  • 72
  • 83