简体   繁体   English

打字稿使用状态<string>参考错误:未定义字符串

[英]Typescript useState<string> ReferenceError: string is not defined

 import React, { FunctionComponent, useState, useEffect } from 'react' const SearchBar: FunctionComponent = () => { const [searchValue, setSearchValue] = useState < string > ('') const [isSuggestionOpen, setIsSuggestionOpen] = useState < boolean > (false) async function showSuggestedWords(event) { setSearchValue(event.target.value) try { const res = await fetch(`https://www.someurl.com/q=${searchValue}`) const suggestedWordsArray = await res.json() // setSuggestedWords(suggestedWordsArray[1].slice(0, 10)) setIsSuggestionOpen(true) return } catch { console.log('error fetching suggested results') setIsSuggestionOpen(false) } } return ( < div > < form action = "/Controller" method = "get" > < input name = "q" type = "text" value = { searchValue } onChange = { showSuggestedWords } autoComplete = "off" autoCapitalize = "off" autoCorrect = "off" spellCheck = "false" placeholder = "Search here" / > < button type = "submit" > Submit < /button> < /form> < /div> ) } ReactDOM.render( < div > < h1 > Hello, world! < /h1> <SearchBar / > < /div>, document.getElementById('root') )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/typescript/4.0.3/typescript.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>

I'm new to typescript so it might be a obvious solution.我是打字稿的新手,所以这可能是一个明显的解决方案。

I have a project made with React, Nextjs and Typescript.我有一个用 React、Nextjs 和 Typescript 制作的项目。

I use typescript to define my state type like so: const [searchValue, setSearchValue] = useState<string>('')我使用打字稿来定义我的状态类型,如下所示: const [searchValue, setSearchValue] = useState<string>('')

And I get an error at runtime: ReferenceError: string is not defined .我在运行时收到一个错误: ReferenceError: string is not defined

I can't figured out what's wrong.我想不通出了什么问题。 Everywhere i check they suggest to define state type like i did above.我检查的每个地方都建议像我上面那样定义状态类型。

Here are my typescript config:这是我的打字稿配置:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "noImplicitAny": true,
    "allowSyntheticDefaultImports": true
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

I don't have webpack config because they are shipped with Nextjs (which is already configured to work with typescript.)我没有 webpack 配置,因为它们随 Nextjs 一起提供(已配置为使用 typescript。)

I do have @types/react @types/react-dom @types/node installed.我确实安装了@types/react @types/react-dom @types/node

Any hint?任何提示?

It turned out it was some mismatching configuration in my babel.config.json原来这是我的babel.config.json一些不匹配的配置

I removed everything and kept just those lines:我删除了所有内容并只保留了这些行:

{
  "presets": ["next/babel"],
  "env": {
    "test": {
      "presets": ["next/babel"]
    }
  }
}

In this way I still can run my Jest tests.这样我仍然可以运行我的 Jest 测试。

Hopefully it can be helpful for someone else in this situation :)希望它可以在这种情况下对其他人有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有flowjs的React useState中的“ReferenceError:未定义字符串” - "ReferenceError: string is not defined" in React useState with flowjs 在对象类型错误中反应打字稿 useState 字符串 - React typescript useState string in object type error 为 typescript 中的 useState 定义所需的类型 - Required type to be defined for useState in typescript React Typescript - 将更多元素从字符串数组添加到 useState 数组 - React Typescript - Add more elements from a string array to useState array React Typescript - 'string' 类型的参数不可分配给 useState 中的类型参数 - React Typescript - Argument of type 'string' is not assignable to parameter of type within useState 如何在 useState 挂钩中创建一个字符串数组并在 TypeScript 中更新同一个数组? - How to create a string array in the useState hook and update that same array in TypeScript? Typescript ReferenceError: google 未定义,但仅在独立文件中 - Typescript ReferenceError: google is not defined, but only in standalone files reactstrap typescript nx`Uncaught ReferenceError: global is not defined` - reactstrap typescript nx `Uncaught ReferenceError: global is not defined` 顺便说一句 useState 有什么不同<string[]> ([]); 和 useState([]); - What is different btw useState<string[]>([]); and useState([]); 如何在 useState 字符串中使用 replace()? - How to use replace() in a useState string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM