Skip to content

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

New issue

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

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

Already on GitHub? # to your account

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?

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

No branches or pull requests

4 participants