【发布时间】:2021-11-23 17:54:43
【问题描述】:
当我想更新我的 State 时,TypesScript 显示错误
TyepScript 在getApiKey 函数内的setapiKey(prompValue); 中用红色下划线prompValue,错误为:
'string | 类型的参数null' 不能分配给“SetStateAction”类型的参数。 类型 'null' 不可分配给类型 'SetStateAction'.ts(2345)
这是什么问题,我该如何解决?
function App() {
const [apiKey, setapiKey] = useState('');
const getApiKey = () => {
const prompValue = prompt('Enter API Key');
setapiKey(prompValue);
}
return (
<div className="App">
<h1>App</h1>
<button onClick={getApiKey}>API</button>
<p>{apiKey}</p>
</div>
);
}
【问题讨论】:
标签: reactjs typescript