Md Toy Blog

React Refs

2019-02-24T22:00:32.169Z

Refs are a way to reference a node, or an instance of a component in our application.

We'll start up with an application that retrieves the value of an input field from the onChange event's target value. It stores that value in the application state.

import React from 'react';

class App extends React.Component {
  constructor() {
    super();
    this.state = {a: ''};
    this.update = this.update.bind(this);
  }
  update(e) {
    this.setState({a: e.target.value});
  }
  render() {
    return {
      <div>
        <input
          type="text"
          onChange={this.update}
        /> {this.state.a}
      </div>
    }
  }
}

export default App