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

React Hooks with Typescript via with Babel 7 #14008

Closed
Raathigesh opened this issue Oct 28, 2018 · 10 comments
Closed

React Hooks with Typescript via with Babel 7 #14008

Raathigesh opened this issue Oct 28, 2018 · 10 comments

Comments

@Raathigesh
Copy link

Do you want to request a feature or report a bug?
Question/Bug

What is the current behavior?

I'm trying to use hooks with a typescript project which uses babel 7 but I'm getting the error Uncaught Error: Hooks can only be called inside the body of a function component.

I guess I'm missing something trivial here but I couldn't figure out what it is. A minimal reproduction of the issue is available at https://github.com/Raathigesh/react-hooks-typescript-with-babel-issue

import React, { useState } from "react";
import ReactDOM from "react-dom";

function Hello() {
  const [isDrawerOpen, setDrawerOpen] = useState(false);
  return <div>Hello</div>;
}

ReactDOM.render(<Hello />, document.getElementById("root"));

Any pointers would be helpful.

@danielkcz
Copy link

danielkcz commented Oct 28, 2018

Strangely I came up with a similar problem for a custom hook trying to wrap the context so I don't need to export whole Context object. It's a bit related to reactjs/rfcs#72

export function useRoot() {
  return useContext(RootContext)
}

Hooks can only be called inside the body of a function component.

@philipp-spiess
Copy link
Contributor

Hey 👋

Can you test if you're running two different instances of react (Someone else also run into this issue at #13991)?

Otherwise a reproduction case would be ideal - Either a small CodeSandbox example or a GitHub repo that we can install and run locally. Your code snippets look alright so I'd like to verify tooling firsts. Thanks for your help!

@lvguoying
Copy link

boy you good

@Raathigesh
Copy link
Author

Hey @philipp-spiess,

I don't think I'm running two different versions of react though.

A reproduction case is available at https://github.com/Raathigesh/react-hooks-typescript-with-babel-issue

Thank you for looking into it.

@philipp-spiess
Copy link
Contributor

@Raathigesh Thanks! This is indeed a tooling issue and I think it's caused by html-webpack-template. If you take a look at the generated HTML, the main JS entry-point is added twice causing two React versions to be loaded:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta content="ie=edge" http-equiv="x-ua-compatible">
    <title>Reignite</title>
  </head>
  <body>
    <div id="root">
    </div>
    <script src="ui.bundle.js" type="text/javascript"></script>
  <script type="text/javascript" src="ui.bundle.js"></script></body>
</html>

If you remove this package (and make sure you still have a mounting point for React), everything works as expected: https://github.com/Raathigesh/react-hooks-typescript-with-babel-issue/pull/1

I recommend you file an upstream issue report with the html-webpack-template maintainers to fix this.

@danielkcz
Copy link

danielkcz commented Oct 29, 2018

@philipp-spiess I've discovered culprit in my case ... it's observer HOC from MobX

https://codesandbox.io/s/vjy5j1xx93

Not sure what is going on there, because regular HOC works just fine as you can see there. I am thinking if I should file a new issue here (obviously it's unrelated to this one) or rather try to solve in on MobX side. Suggestions?

Edit: created issue on MobX side

@Raathigesh
Copy link
Author

@philipp-spiess Oh right! Thank you so much for looking into this.

@hrgui
Copy link

hrgui commented Oct 29, 2018

Not sure what is going on there, because regular HOC works just fine as you can see there. I am thinking if I should file a new issue here (obviously it's unrelated to this one) or rather try to solve in on MobX side. Suggestions?

I think Hooks don't work with components that are wrapped with HOCs, since HOCs most often wrap functional components with a class component.

Example: https://codesandbox.io/s/mm47kpqnq9 <- just click the button to crash the page.

observer from mobx-react seems like it's wrapping the functional component in a class component according to https://github.com/mobxjs/mobx-react/blob/master/src/observer.js#L330-L344

@philipp-spiess
Copy link
Contributor

@FredyC Got it! I saw you already filed an upstream issue - I think this needs to be resolved on the MobX side.

@hrgui In your example you're trying to use a React Hook in a Class Component which is not supported as the warning already indicates. The implementation would clash with setState in classes.

I think this resolves all issues in this ticket. I'm going to close it but if you find a bug feel free to open a new ticket and include a reproduction case.

@526117
Copy link

526117 commented Jun 8, 2019 via email

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

6 participants