简体   繁体   English

React Typescript - 类型参数不可分配给类型参数

[英]React Typescript - Argument of type is not assignable to parameter of type

I have a demo here这里有演示

It's a React app using Typescript and hooks to capture entries into form that are simple displayed below.这是一个 React 应用程序,使用 Typescript 和钩子将条目捕获到下面简单显示的表单中。

Here in Stackblitz it works but locally I am using VS code and I get an error for setUser(userData);在 Stackblitz 中,它可以工作,但在本地我使用的是 VS 代码,我收到setUser(userData); the error is错误是

const userData: {
    username: string;
    password: string;
    prevState: null;
}
Argument of type '{ username: string; password: string; prevState: null; }' is not assignable to parameter of type '(prevState: null) => null'.
  Type '{ username: string; password: string; prevState: null; }' provides no match for the signature '(prevState: null): null'.ts(2345)

How can I fox this typescript error我怎样才能解决这个打字稿错误

const [user, setUser] = useState(null);

Since you havn't given this a type, typescript has to try to infer it.因为你没有给它一个类型,typescript 必须尝试推断它。 It sees you passed in a null, so it assumes this state is (and always will be) null.它看到您传入了一个空值,因此它假定此状态为(并且始终为)空值。 Instead, you need to specify the type, as in:相反,您需要指定类型,如:

interface UserData {
  username: string;
  password: string;
  prevState: null
}

//...
const [user, setUser] = useState<UserData | null>(null);

If you fetching data from API you need this:如果你从 API 获取数据,你需要这个:

import React, { useState, useEffect } from 'react'
import axios from 'axios';

const MyComponent = () => {
const [data, setData] = useState<any | null>(null);
const fetchData = () => {
(...) //Axios.get() or async if u await data.
setData(fetchData)
}
}
useEffect(() =>{
    fetchData()
}, [])

暂无
暂无

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

相关问题 React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' &#39;x&#39;类型的React Typescript参数不可分配给&#39;SetStateAction&#39;类型的参数<x]> &#39; - React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>' 反应,typescript “数字”类型的参数不可分配给“ChangeEvent”类型的参数<htmlinputelement> '</htmlinputelement> - react, typescript Argument of type 'number' is not assignable to parameter of type 'ChangeEvent<HTMLInputElement>' React,typescript - 'string | 类型的参数 null' 不能分配给“字符串”类型的参数 - React, typescript - Argument of type 'string | null' is not assignable to parameter of type 'string' React和Typescript:类型参数不能分配给&#39;EventListenerOrEventListenerObject&#39;类型的参数 - React and Typescript: Argument of type is not assignable to parameter of type 'EventListenerOrEventListenerObject' 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() =&gt; 状态属性)' - React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)' React Typescript - 'string' 类型的参数不可分配给 useState 中的类型参数 - React Typescript - Argument of type 'string' is not assignable to parameter of type within useState React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type “文件”类型的参数不能分配给“从不”类型的参数。 与 TypeScript 反应 - Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM