简体   繁体   English

反应 Typescript 使用状态

[英]React Typescript useState

I'm trying to create a custom hook that accepts a non-mandatory date as a prop, I need to call this hook with a different date or without it, and if there is a date, I should return exactly the type of this date, for example f(string) => string[].我正在尝试创建一个接受非强制性日期作为道具的自定义钩子,我需要用不同的日期或没有它来调用这个钩子,如果有日期,我应该准确地返回这个日期的类型,例如 f(string) => string[]。 I'm sorry, I can't show you the real code, but I created an equivalent code.对不起,我不能给你看真正的代码,但我创建了一个等效的代码。

import { useStatem, useEffect } from "react";

type IProps<T> = {
  isValid: boolean;
  testData?: T[];
};
interface IState<T> {
  error: boolean;
  loading: boolean;
  data: T[];
}
type InitialTypes = "john" | 55;

export function useSome<T extends InitialTypes>({
  testData,
  isValid,
}: IProps<T>) {
  const [value, setValue] = useState<IState<T>>({
    error: false,
    loading: false,
    data: [],
  });
  useEffect(() => {
    // some logic
    setValue((prevState) => ({ ...prevState, data: ["john"] }));
  }, []);

  return [value.data];
}

when I try to setValue I get error:当我尝试 setValue 时出现错误:

TS2345: Argument of type '(prevState: IState<T>) => { data: "john"[]; error: boolean; loading: boolean; }' is not assignable to parameter of type 'SetStateAction<IState<T>>'.   
Type '(prevState: IState<T>) => { data: "john"[]; error: boolean; loading: boolean; }' is not assignable to type '(prevState: IState<T>) => IState<T>'.     
Call signature return types '{ data: "john"[]; error: boolean; loading: boolean; }' and 'IState<T>' are incompatible.       
The types of 'data' are incompatible between these types.         
Type '"john"[]' is not assignable to type 'T[]'.           
Type '"john"' is not assignable to type 'T'.             
'"john"' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'InitialTypes'.

Thanks a lot.非常感谢。

As per your code, the issue is caused by const [value, setValue] = useState<IState<T>>({error: false, loading: false, data: []});根据您的代码,问题是由const [value, setValue] = useState<IState<T>>({error: false, loading: false, data: []}); line.线。

In your IState type you have a generic T which is used to determine the type of array hold by data variable as follow:在您的IState类型中,您有一个通用T ,用于确定data变量持有的数组类型,如下所示:

interface IState<T> {
    error: boolean,
    loading: boolean,
    data: T[],
}

Whereas, in const [value, setValue] = useState<IState<T>>({error: false, loading: false, data: []});而在const [value, setValue] = useState<IState<T>>({error: false, loading: false, data: []}); , you have forgot to specify/provide the generic type T. ,您忘记指定/提供泛型类型 T。

So, instead of useState<IState<T>> it should be either useState<IState<string>> or useState<IState<InitialTypes>>因此,应该是useState<IState<string>>useState<IState<InitialTypes>>而不是useState<IState<T>> >>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM