简体   繁体   English

Typescript 抱怨 React useState setter `Argument of type 'Dispatch <setstateaction<never[]> &gt;' 不可分配给参数</setstateaction<never[]>

[英]Typescript complaining about React useState setter `Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of

Presume I have this react component假设我有这个反应组件

export default function Restaurants() {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    fetchUsers().then(setUsers);
  })
  ...

and User.ts isUser.ts

interface User { name: string; }
export function fetchUsers(): Promise<User[]> { return fetch(...).then(r => r.json()) }

TypeScript is throwing Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of type '(value: User[]) => void | PromiseLike<void>'. Types of parameters 'value' and 'value' are incompatible. Type 'IRestaurant[]' is not assignable to type 'SetStateAction<never[]>'. Type 'IRestaurant[]' is not assignable to type 'never[]'. Type 'IRestaurant' is not assignable to type 'never'.ts TypeScript 正在抛出Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of type '(value: User[]) => void | PromiseLike<void>'. Types of parameters 'value' and 'value' are incompatible. Type 'IRestaurant[]' is not assignable to type 'SetStateAction<never[]>'. Type 'IRestaurant[]' is not assignable to type 'never[]'. Type 'IRestaurant' is not assignable to type 'never'.ts Argument of type 'Dispatch<SetStateAction<never[]>>' is not assignable to parameter of type '(value: User[]) => void | PromiseLike<void>'. Types of parameters 'value' and 'value' are incompatible. Type 'IRestaurant[]' is not assignable to type 'SetStateAction<never[]>'. Type 'IRestaurant[]' is not assignable to type 'never[]'. Type 'IRestaurant' is not assignable to type 'never'.ts

I tried const [users, setUsers]: [User[], Function] = useState([]);我试过const [users, setUsers]: [User[], Function] = useState([]); but that also didn't solve the issue How can I solve this issue?但这也没有解决问题我该如何解决这个问题?

You need TS to be able to infer at the time that you call useState what sort of value it will contain.您需要 TS 能够在您调用useState推断出它将包含什么样的值。

Since it's going to be an array of User s, use that for the generic:由于它将是User的数组,因此将其用于泛型:

const [users, setUsers] = useState<User[]>([]);

I'd also highly recommend not ignoring errors - unhandled rejections should be avoided whenever possible.我还强烈建议不要忽略错误 - 应尽可能避免未经处理的拒绝。

fetchUsers()
  .then(setUsers)
  .catch(handleErrors);

You also probably only want to fetch the users once , rather than every time the component mounts.您也可能只想获取用户一次,而不是每次安装组件时。

useEffect(() => {
  fetchUsers()
    .then(setUsers)
    .catch(handleErrors);
}, []);

暂无
暂无

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

相关问题 Typescript/React-Native:'string | 类型的参数 null' 不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Typescript/React-Native: Argument of type 'string | null' is not assignable to parameter of type 'SetStateAction<never[]>' 类型参数不可分配给“SetStateAction”类型的参数<never[]> &#39;。 在反应中 - Argument of type is not assignable to parameter of type 'SetStateAction<never[]>'. in React &#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]>' React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' React useState 挂钩错误:“xxx”类型的参数不可分配给“SetStateAction”类型的参数<xx> '</xx> - React useState hooks error: Argument of type 'xxx' is not assignable to parameter of type 'SetStateAction<xx>' “未知 []”类型的参数不可分配给“SetStateAction”类型的参数<never[]> '</never[]> - Argument of type 'unknown[]' is not assignable to parameter of type 'SetStateAction<never[]>' React js Typescript “字符串”类型的参数不可分配给“SetStateAction”类型的参数<number> '</number> - React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>' React Typescript - 'string' 类型的参数不可分配给 useState 中的类型参数 - React Typescript - Argument of type 'string' is not assignable to parameter of type within useState 如何修复 React TypeScript 错误:'""' 类型的参数不可分配给 'SetStateAction 类型的参数<undefined> '</undefined> - How to fix React TypeScript error: Argument of type '""' is not assignable to parameter of type 'SetStateAction<undefined>' 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() =&gt; 状态属性)' - React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM