Monday, April 16, 2018

React refs

Refs in React is a shortcut way to access a specific DOM node or a React component.
 

There are two types of refs in react, string refs and callback refs.
 

In string refs we set the ref attribute to a name (string) and later access the element/component using the referenced name.

To access simple DOM elements using string refs we can use the name of the ref as follows.
this.refs..value.

To access react components using string refs we need to get the instance of the component using ReactDOM.findDOMNode as follows.
var instance = ReactDOM.findDOMNode(this.refs.);

The other type of ref is the callback refs.
In callback refs we set the ref attribute to a function which receives the component/element reference as an argument and sets the reference.


Search Flipkart Products:
Flipkart.com

No comments: