What's a bad practice with refs in React? What's a bad practice with refs in React? reactjs reactjs

What's a bad practice with refs in React?


React requires you to think the react way and refs are kind of a backdoor to the DOM that you almost never need to use. To simplify drastically, the react way of thinking is that once state changes, you re-render all the components of your UI that depend on that state. React will take care of making sure only the right bits of the DOM are updated, making the whole thing efficient and hiding the DOM from you (kinda).

For example, if your component hosts an HTMLInputElement, in React you'll wire up an event handler to track changes to the input element. Whenever the user types a character, the event handler will fire and in your handler you'll update your state with the new value of the input element. The change to the state triggers the hosting component to re-render itself, including the input element with the new value.

Here's what I mean

import React from 'react';import ReactDOM from 'react-dom';class Example extends React.Component {    state = {      inputValue: ""    }    handleChange = (e) => {      this.setState({        inputValue: e.target.value      })    }    render() {        const { inputValue } = this.state        return (           <div>            /**.. lots of awesome ui **/            /** an input element **/            <input value={inputValue} onChange={this.handleChange}} />            /** ... some more awesome ui **/          </div>       )  }}ReactDOM.render( <Example />, document.getElementById("app") );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><div id="app"></div>