[英]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.