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

TypeError: Cannot read property 'bind' of undefined when using Flow #5176

Closed
Gpx opened this issue Sep 30, 2018 · 6 comments
Closed

TypeError: Cannot read property 'bind' of undefined when using Flow #5176

Gpx opened this issue Sep 30, 2018 · 6 comments
Milestone

Comments

@Gpx
Copy link

Gpx commented Sep 30, 2018

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

flow bind

Environment

Environment:
  OS:  macOS Sierra 10.12.6
  Node:  10.11.0
  Yarn:  Not Found
  npm:  6.4.1
  Watchman:  4.7.0
  Xcode:  Xcode 9.2 Build version 9C40b
  Android Studio:  Not Found

Packages: (wanted => installed)
  react: ^16.5.2 => 16.5.2
  react-dom: ^16.5.2 => 16.5.2
  react-scripts: 2.0.1 => 2.0.1

Steps to Reproduce

  1. Create a component like this one
  class Foo extends React.Component {
    constructor(props) {
      super(props)
      this.foo = this.foo.bind(this)
    }
    foo: () => ()
    foo() {}
    render() { return <div /> }
  }
  1. Run the tests for Foo
  2. TypeError: Cannot read property 'bind' of undefined

Expected Behavior

The file should run in the tests

Actual Behavior

I think Flow's syntax is confusing the tests. Note that this same code was working in the previous version of CRA

Reproducible Demo

Simply clone https://github.com/Gpx/cra2-bind and run the tests

@gaearon
Copy link
Contributor

gaearon commented Sep 30, 2018

This is because Babel 7 changed the behavior of class properties so that an empty “definition” like this is treated as assigning undefined. AFAIK this is closer in behavior to the latest specification.

We’ll need to mention this in migration notes.

Still I’m not sure if this is correct. We’ll need more eyes on this.

Some discussion: babel/babel#8417.

Apparently this could solve it? xing/hops@9cf7791
Maybe our preset should do this.

@Gpx
Copy link
Author

Gpx commented Sep 30, 2018

The thing is that Flow is asking me to add a type definition to foo and I don't know how else to do it

@gaearon
Copy link
Contributor

gaearon commented Sep 30, 2018

Yeah, I understand. We'll need to dig deeper into this.

@Gpx
Copy link
Author

Gpx commented Sep 30, 2018

Ok, thanks. Let me know if I can help somehow

@bugzpodder
Copy link

@Gpx try this. This uses the flow commenting method. Obviously not ideal but it should provide with a workaround.

diff --git a/src/App.js b/src/App.js
index d70a6ba..8c37374 100644
--- a/src/App.js
+++ b/src/App.js
@@ -10,7 +10,7 @@ class App extends Component {
     this.foo = this.foo.bind(this);
   }
 
-  foo: void => void;
+  /*::  foo: void => void */
   foo() {}
 
   render() {

@Gpx
Copy link
Author

Gpx commented Sep 30, 2018

@bugzpodder yep, that works, unfortunately, in my app, we use prettier which converts comments into standard type definitions. I think we'll wait to update CRA until this issue has been closed

@Timer Timer added this to the 2.0.0 milestone Sep 30, 2018
@lock lock bot locked and limited conversation to collaborators Jan 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants