Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about refs? #183

Closed
cymen opened this issue Jun 27, 2015 · 8 comments
Closed

What about refs? #183

cymen opened this issue Jun 27, 2015 · 8 comments

Comments

@cymen
Copy link

cymen commented Jun 27, 2015

If I use a <Connector> around some components that have a ref attribute, I can no longer access it via this.refs. I just refactored a bit to fix it -- I am guessing this is kind of in the vain of smart versus dumb components. Or am I missing something simple?

@emmenko
Copy link
Contributor

emmenko commented Jun 27, 2015

smart versus dumb components

I think so yes, your smart component with the Connector should just render dumb components and pass the needed props to them.

@johanneslumpe
Copy link
Contributor

@cymen you can use a ref callback for this :)

const decorator = (Base) => {
  return class {
    render() {
      return <Base {...this.props} />
    }
  }
}

@decorator
class Test {

  render () {
    return <div ref={this.props.refCallback}>my div</div>;
  }

}


class Wrapper {

  componentWillMount() {
    this.nestedRef = null;
  }

  componentDidMount() {
    console.log(React.findDOMNode(this.nestedRef));
  }

  setNestedRef = (ref) => {
    this.nestedRef = ref;
  }

  render() {
    return <Test refCallback={this.setNestedRef} />
  }
}

The above will log the div. Maybe this helps?

This uses a decorator, but It would probably just work as well if you passed a callback to your components which you nested inside Connector. Inside the callback you can then assign the ref back to your component.

@gaearon
Copy link
Contributor

gaearon commented Jun 27, 2015

Do you really need a nested ref?

If you just want to access the DOM node, you can do findDOMNode on the outer component and it will work just fine. (They will point to the same node.)

If you want to call methods, consider using props as the declarative interface instead. See also https://github.com/jsstyles/react-jss/issues/15 for a discussion about this.

Finally, if you're certain you need a ref, yeah, you can grab a nested ref like @johanneslumpe suggested in #183 (comment).

@gaearon
Copy link
Contributor

gaearon commented Jul 7, 2015

Closing, please let me know if something isn't clear.

@gaearon gaearon closed this as completed Jul 7, 2015
@danikenan
Copy link

Sorry but I do not see how encouraging the use of 'findDOMNode' in such a trivial case can even be considered a solution. This is an anti-pattern! It's returns us to days of jquery where we need to access all element and know exactly the hierarchy of the nodes. Your docs rightfully read :

'findDOMNode() is an escape hatch... use of this escape hatch is discouraged because it pierces the component abstraction'

@gaearon
Copy link
Contributor

gaearon commented Dec 29, 2015

Sorry but I do not see how encouraging the use of 'findDOMNode' in such a trivial case can even be considered a solution. This is an anti-pattern! It's returns us to days of jquery where we need to access all element and know exactly the hierarchy of the nodes.

I’m not sure what you mean. I’m saying that, if you need a reference to the rendered node (for example, to calculate its offset and size), you can use findDOMNode(this). I’m not suggesting you to “go deeper” or mutate it.

That said if you’re not happy with this, just use ref callback props as suggested above.

@xvalentino
Copy link

I don't think we should depend on findDOMNode(this) seeing as it's not applicable to React-Native.

@jimbolla
Copy link
Contributor

jimbolla commented Oct 6, 2016

@xvalentino findDOMNode is not necessary. See the bits related to getWrappedInstance in the API docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants