React Hooks with Typescript via with Babel 7
See original GitHub issueDo 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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Setup a React App using Webpack, Babel and TypeScript
This article is part of a two part series on how to configure a React app from scratch with Webpack and Babel and...
Read more >Working With React Hooks and TypeScript - Toptal
This article will introduce you to hooks in React and demonstrate how you can harness the potential of hooks in a code-complete tutorial....
Read more >Setting TypeScript For Modern React Projects Using Webpack
In this tutorial, we will learn what TypeScript is and how to work with it in a React project. By the end, we'll...
Read more >babel/preset-react
@babel/preset-react ... This preset always includes the following plugins: ... And with the development option: Classic runtime adds: ... Automatic runtime (since v7....
Read more >Using Typescript with Modern React (i.e. Hooks, Context ...
Using Typescript with modern React (i.e. hooks, context, suspense). Learn how to add TypeScript to a create- react -app project as well as ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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: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.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!