nytimes / react-tracking

🎯 Declarative tracking for React apps.
https://open.nytimes.com/introducing-react-tracking-declarative-tracking-for-react-apps-2c76706bb79a
Other
1.88k stars 123 forks source link

Can't access reffed component #87

Closed dutzi closed 4 years ago

dutzi commented 6 years ago

Hi guys,

We're having issues using react-tracking with reffed components.

The following example illustrates this:

@track({})
export default class ChildComponent extends Component {
    focus() {
        ...
    }
    ...
}

export default class ParentCompoent extends Component {
    componentDidMount() {
        this.child.focus()
    }
    render() {
        <ChildComponent ref={el => this.child = el}/>
    }
}

ParentComponent can't call ChildComponent's focus method since @track annotation wraps the component without allowing access to the component itself.

React-redux solves this with an option called withRef and and exposing a function called getWrappedInstance, would be great if could also see something similar here.

Thank you!

tizmagik commented 6 years ago

Ah, yes, thanks for the issue, that makes sense. Although I think the React.forwardRef API might be a better fit for this, https://reactjs.org/docs/forwarding-refs.html

dutzi commented 5 years ago

Hey @tizmagik, sorry for nagging but any idea when this fix will go live or should we implement it for now? (We just have something that depends on it waiting to go live)

tizmagik commented 5 years ago

Feel free to submit a PR, otherwise I can try and take a look at it this week

kelyvin commented 5 years ago

+1

AliceMarshall commented 4 years ago

Hi, is there anymore work being done on this? I see you made a PR back in 2018 for a fix, would really like a way to forwardRefs with HOC tracking.

tizmagik commented 4 years ago

Yeap unfortunately I haven't had a chance to land that PR. If you're interested, feel free to pickup where that PR left off and/or start a new one. I think it's pretty close, I just haven't had time to focus on that 😞