question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to retrieve ref to input when HOC used

See original GitHub issue

When defining an input class as lower order component of Formsy, how does one go about retrieving a ref to the underlying input component?

If an input class is simply:

class Input {
  render() {
    return (<div><label ...props>{this.props.caption}</label><input ...props/></div>);
}

export default Formsy.HOC(Input);

And the form:

class AForm {
  componentWillReceiveProps() {
    // How to access the input component? this.refs.input refers to the HOC of Input.
  }

  render() {
    return (
      <Form>
        <Input ref="input" ...props/>
      </Form>
    );
  }
}

Say I would like the Input class to allow for the focus to be set programmatically on its {input,textarea} element; how would I go about doing so? How would such an interface be exposed from Input above?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7

github_iconTop GitHub Comments

12reactions
clayne11commented, Feb 18, 2016

The HOC passes all props through to the component it is wrapping, so you can simply use a callback ref to achieve your goal.

https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute

3reactions
briansotodocommented, Apr 20, 2018

use callback instead. here is example

Foo and App are not HOC @ggd543

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass inner ref and use it inside a HOC - Stack Overflow
I want to forward the inner Input ref through FancyInput , and also use the same ref inside the FancyInput , like calling...
Read more >
Using React.forwardRef() and an HOC on the same component
Focus management in React currently has one solution: refs. If you want a function component to accept a ref, you should use React....
Read more >
Forwarding Refs - React
In the example below, FancyButton uses React.forwardRef to obtain the ref passed to it, and then forward it to the DOM button that...
Read more >
How to Use React Refs - Ross Bulat - Medium
Refs at a high level ... Refs are created using React.createRef() , and are assigned to class properties. In the above example the...
Read more >
Fullstack React's Guide to using Refs in React Components
A guide to React Refs - Forward Refs (HOC) ... In the example app above, every keypress in the input field is logged...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found