React - Ref

React Mapping Html Javascript

About

A ref is a reference to:

Parent components can get the element by reference and manipulate them

Usage

  • Managing focus, text selection, or media playback.
  • Triggering imperative animations.
  • Integrating with third-party DOM libraries.

Example

Dom element

input example with a Uncontrolled Component

Note that in React, you define the default value with the attribute defaultValue and not value.

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.input = React.createRef();
    console.log("At its creation, the ref.current value is "+this.input.current);
  }

  handleSubmit(event) {
    console.log('The ref gives access to the HTML element via current: ' + this.input.current.__proto__.toString());
    console.log('where you have access to all HTML attribute such as the value');
    console.log('Example: the name submitted was: ' + this.input.current.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" ref={this.input} defaultValue="ChangeMe" />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

ReactDOM.render(
  <NameForm />,
  document.getElementById('root')
);
<div id="root"></div>

Management

Creation

Refs are created using React.createRef() (since 16.3)

Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component.

Refs are attached to React elements via the ref attribute.

Something like that:

const ref = React.createRef();
return ( 
   <input type="text" ref={ref} defaultValue="ChangeMe" />
)

Hook

React has a Hook. See useRef

Documentation / Reference





Recommended Pages
Card Puncher Data Processing
D3 - React

D3 in react By using the ref attribute
React Mapping Html Javascript
React - Children Element

When you create an element, you pass children element that will build the React DOM tree. componentyou create a component Related: See also The below example codes are created with JSX. pure javascript...
React Mapping Html Javascript
React - Composite Component (Layout) / Container Component

React Layout / Composition - There is three methods to create a composite component from simple component / or composite (decorator) Props: via props but alsoRefs (They provide a...
React Mapping Html Javascript
React - Forwarding Ref

The forwardRef function wraps a element to add to the props a ref that is generated by React. This ref is called a forwarding ref because you pass it along to use it in one of its child components in...
React Mapping Html Javascript
React - Input Element

This page is the handling of input element in React. The react forms components input accept a value attribute that is used to implement a controlled component. A React form: written as a class...
React Mapping Html Javascript
React - Uncontrolled Component (DOM State)

An uncontrolled component is a component such as form where the state is handled by the DOM itself (not react) controlled component if: you need to write an very difficult event handler (for...



Share this page:
Follow us:
Task Runner