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

How to safely access instance properties declared in a class component with createRef #108

Open
urielvan opened this issue Nov 27, 2018 · 3 comments

Comments

@urielvan
Copy link

urielvan commented Nov 27, 2018

I have a child component connected by redux

class Child extends React.Component {
    foo () {}
}

export default connect()(Child);

and a parent contains it

class Parent extends React.Component {
    private childRef: React.RefObject<Child> = React.createRef()

    bar () {
        if (this.childRef.current) {
            /*
             * here typescript complains that 
             * Property 'foo' does not exist on
             * type 'ConnectedComponentClass<typeof Child...'
             */
            this.childRef.current.foo();
        }
    }

    render () {
        return (
            <Child ref={this.childRef} />
        );
    }
}

I've tried to set the generic type

<React.ComponentType<Child>>

explicitly when exporting child component, but still not working.

@IssueHuntBot
Copy link

@issuehuntfest has funded $20.00 to this issue. See it on IssueHunt

@piotrwitek piotrwitek changed the title Calling connected child instance method How to safely access instance properties declared in a class component with createRef Apr 12, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $20.00 to this issue.


@isolin
Copy link

isolin commented Apr 13, 2020

I am facing the same problem. Any solution / work around so far?

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

4 participants