Skip to content

Latest commit

 

History

History
26 lines (15 loc) · 882 Bytes

11.Refs.md

File metadata and controls

26 lines (15 loc) · 882 Bytes

Jam3 Lessons - React

Refs

So far, the only way that parent components can trigger changes within their child components is to pass new props. Another way to communicate with child components is through refs.

Through the ref attribute, the parent component can gain access to the child component or element.

Here the property this.textInput holds the input element.

<input type="text" ref={(input) => { this.textInput = input; }} />

When passed as a prop to a custom component its callback argument would be the mounted instance of the component.

<CustomInput ref={(input) => { this.textInput = input; }} />

As a rule of thumb, try to use props before trying to use refs, so as to make it clear where the state of a component is owned.

References