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

Flow errors with styled component ref prop #332

Open
sandgraham opened this issue Jul 26, 2019 · 1 comment
Open

Flow errors with styled component ref prop #332

sandgraham opened this issue Jul 26, 2019 · 1 comment
Labels

Comments

@sandgraham
Copy link

Flow errors when trying to pass a ref to a styled component.

// flow works
class Foo extends React.Component<any, any> {
  ref = React.createRef<HTMLInputElement>();
  render() {
    return <input ref={this.ref} />;
  }
}

// flow works
import {styled} from 'styletron-react';
const Input = styled('input', {});
class Boo extends React.Component<any, any> {
  ref = React.createRef<HTMLInputElement>();
  render() {
    return <Input $ref={this.ref} />;
  }
}

// code works, flow fails
import {styled} from 'styletron-react';
const Input = styled('input', {});
class Boo extends React.Component<any, any> {
  ref = React.createRef<HTMLInputElement>();
  render() {
    return <Input ref={this.ref} />; // flow complains about ref type
  }
}

Here is the flow error output:

Cannot create Input element because:
 • Either undefined [1] is incompatible with null [2] in property current of property ref.
 • Or a call signature declaring the expected parameter / return type is missing in object type [3] but exists in
   function type [4] in property ref.
 • Or object type [5] is not a React component.

        src/App.js
         22│   render() {
         23│     const Input = styled("input", {});
         24│     return (
         25│       <Input ref={this.ref} onChange={() => console.log(this.ref.current)} />
         26│     );
         27│   }
         28│ }

        /private/tmp/flow/flowlib_289644ca/react.js
    [4] 197│   | ((React$ElementRef<ElementType> | null) => mixed)
           :
 [3][2] 246│   ): {|current: null | T|};

        node_modules/styletron-react/src/types.js
 [1][5]  48│ export type StyletronComponent<Props> = StatelessFunctionalComponent<Props> & {
         49│   __STYLETRON__: any,
         50│ };
@KosGrillis
Copy link

Any workarounds for this?

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

No branches or pull requests

2 participants