Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React useRef Hook with Flow Typings

I'm using React useRef with Flow typings and I'm trying to write a wrapper component for a third party web components library.

The web component expects a changeCallback function and I'm using the ref to assign it to the ref.

function RadioButtonGroup({ onChange, children }) {
    const ref: { current: null | ElementRef<ElementType> = React.useRef(null);

    React.useEffect(() => {
        if (ref.current) ref.current.changeCallback = onChange;
    }, [onChange]);

    return <web-component ref={ref}>{children}</web-component>
}

Since HTMLElement does not contain a property called changeCallback flow throws an error.

Cannot assign handleChange to ref.current.changeCallback because property changeCallback is missing in HTMLElement

I tried extending "ElementType" with the property like this

ElementRef<ElementType & { changeCallback: Function }>

But this results in the following error:

Cannot instantiate ElementRef because object type [1] is not a React component.

The web component does not fire the "change" event on change. It executes the function changeCallback. Here's the documentation for the library.

// MyComponent.js

class MyComponent extends Component {

    constructor() {
        // ...

        // Create a ref
        this.sdxSelectEl = React.createRef();
    }

    componentDidMount() {
        // Attach callback here to ref
        this.sdxSelectEl.selectCallback = (selection) => console.log(selection);
    }

    render() {
        // ...
        <sdx-select ref={el => (this.sdxSelectEl = el)} />
        // ...
    }
}
like image 382
Timo Jokinen Avatar asked Sep 26 '19 12:09

Timo Jokinen


1 Answers

The solution is to call useRef with an explicit type argument to represent the expected type:

const ref = React.useRef<null | (HTMLElement & { changeCallback: Function })>(null);
like image 199
Timo Jokinen Avatar answered Sep 19 '22 19:09

Timo Jokinen