Extracting values from refs pointing to form inputs I have a form component with multiple refs pointing to form inputs. On submit, those inputs' values need to be passed the into form callback, structured as an object.

Currently, it's achieved like this:

class MyComponent extends React.Component {
    render() {
        return (
            &lt;form onSubmit={() =&gt; this.onSubmit()}&gt;
                &lt;input ref="firstName"&gt;
                &lt;input ref="lastName"&gt;
                &lt;input ref="age"&gt;
                &lt;input ref="sex"&gt;
            &lt;/form&gt;
        );
    }
    onSubmit() {
        let firstName = this.refs.firstName.value.trim();
        let lastName = this.refs.lastName.value.trim();
        let age = this.refs.age.value.trim();
        let sex = this.refs.sex.value.trim();

        this.props.onSubmit({
            firstName,
            lastName,
            age,
            sex
        });
    }
}


These lines let firstName = this.refs.firstName.value.trim(); look like a code duplication to me. Could they be rewritten into something more elegant? ES2015 answers are welcome.
These lines let firstName = this.refs.firstName.value.trim(); look like a code duplication to me. Could they be rewritten into something more elegant?


Looks ok to me. These fields are too few to be generalized by some generalization logic.

Now if the form does grow, you can just get a reference to the &lt;form&gt; and do a quick querySelectorAll for all input elements, including select and textarea that have a name.

var inputs = this.refs.form.querySelectorAll('input[name]');


You can then loop through them, get the name and value and use it as key and value for your object. For simple cases, this works. A slightly more complicated case is handling complex names like foo[a] and foo[b] which is usually interpreted as {foo: {a : 'value', b: 'value'}}.