Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
How to implement a forward ref in React0.14?

I want to get the ref of the children's component.The nested relationship between the components is shown below:

<Demo1 /> ---> <Demo2 /> ---> <Demo3 />

how to get the ref of Demo3 in Demo1?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can pass the ref inside of the props:

// -- sub-sub component
var SubSub = React.createClass({
    render: function() {
        return <div><sub ref={ this.props.subSubRef }>Sub-sub component</sub></div>;
    }
});

// -- sub component
var Sub = React.createClass({
    render: function() {
        return <div><em>Sub component</em><SubSub subSubRef={ this.props.subRef } /></div>;
    }
});

// -- root component
var Main = React.createClass({
    render: function() {

        // -- get the ref from the child
        const refCallback = function( domRef ){
            this._subSubDomRef = domRef;
        }.bind( this );

        // -- example: accessing the dom element in some way
        const handleClick = function(){
            if( this._subSubDomRef ){
                this._subSubDomRef.innerHTML = this._subSubDomRef.innerHTML + '.';
            }
        }.bind( this );

        // --
        return <div>
            <strong onClick={ handleClick }>Main component</strong>
            <Sub subRef={ refCallback } />
        </div>;
    }
});

(Note that you might run into conflicts in the case that some library that you are using, uses the same prop name as you do. That's one reason why forwardRef is recommended)

Further information:

The official React documentation mentions forwarding refs prior to the availability of forwardRef under exposing-dom-refs-to-parent-components, and recommends the example in dom_ref_forwarding_alternatives_before_16.3.md.

Unfortunately this example uses createRef, which is also not available before v16.3. You need to follow the comments there and adapt the example accordingly.

The React documentation for v0.14 can be found at web.archive.org.

forwardRef and createRef were introduced in version 16.3.0 (March 29, 2018):

  • 2018-02-06 createRef (8dc8f88d5ae9fb96934ba43e3842b5dcf4074afd)
  • 2018-03-14 forwardRef 16.3.0-alpha.2 (bc70441c8b3fa85338283af3eeb47b5d15e9dbfe)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!