Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can import React, but not useState?

I am attempting to write a cordova app with ts and react by starting with the boilerplate: https://github.com/davidgerrard/cordova-react-typescript-webpack-boilerplate

The cloned code works fine, but when I attempt to write my own component I start with a typical:

import React, { useState } from 'react';

It imports React just fine, but tells me

Module '"react"' has no exported member 'useState'

I am used to react on the web, so is there some different way to do this for cordova? Or is there something that may need to be configured and/or updated?

like image 514
TheHeadlessSourceMan Avatar asked Nov 18 '25 01:11

TheHeadlessSourceMan


2 Answers

For me, there was a mismatch between my react version (17.0.2) and my @types/react version (17.0.37). Installing the same version of types solved my issue.

npm i --save-dev @types/[email protected]
like image 58
joematune Avatar answered Nov 20 '25 16:11

joematune


TL;DR? User zero298 nailed it.

If you ever get this error it is because you are using an old pre-hooks version of React.

The easiest fix is to simply run "npm upgrade" (or yarn, or whatever your preferred package manager is)

I hope others googling this error can learn from my mistakes.

like image 32
TheHeadlessSourceMan Avatar answered Nov 20 '25 16:11

TheHeadlessSourceMan