Skip to content

Latest commit

 

History

History
51 lines (45 loc) · 798 Bytes

refCB.md

File metadata and controls

51 lines (45 loc) · 798 Bytes

Fantastic Tip

refCB help get rid of findDOMNode

Before:

const Input = (props) =>
{
    return <input />;
}

class MyInput extends Component 
{
  componentDidMount()
  {
      findDOMNode(this.el).focus();
  } 

  render()
  {
      return <Input ref={el=>this.el=el} />;
  }
}

After:

const Input = (props) =>
{
    let {refCb, ...others} = props;
    if (refCb) {
        others.ref = refCb;
    }
    return <input {...others} />;
}

class MyInput extends Component 
{
  componentDidMount()
  {
     this.el.focus();
  } 

  render()
  {
      return <Input refCb={el=>this.el=el} />;
  }
}

Merge Ref