【问题标题】:React, Typescript, Hooks - destructuring data from hook, TS2339: Property 'data' does not exist on typeReact,Typescript,Hooks - 从钩子中解构数据,TS2339:类型上不存在属性“数据”
【发布时间】:2020-07-31 05:00:00
【问题描述】:

我在从我自己在 React 中创建的 Hook 中解构 Typescript 数据对象时遇到问题。

export interface InitialState {
  pokemonListLoading: false;
  pokemonListLoadingFailed: false;
  data: [];
}

interface FetchPokemonList {
  type: typeof FETCH_POKEMON_LIST;
}

interface FetchPokemonListSuccess {
  type: typeof FETCH_POKEMON_LIST_SUCCESS;
  payload: PokemonList;
}

...

export type PokemonListActionTypes = FetchPokemonList | FetchPokemonListSuccess | FetchPokemonListError;

const dataFetchReducer = (state: InitialState, action: PokemonListActionTypes) => {
  switch (action.type) {
    case FETCH_POKEMON_LIST:
      return {
        ...state,
        pokemonListLoading: true,
        pokemonListLoadingFailed: false,
      };
    case FETCH_POKEMON_LIST_SUCCESS:
      return {
        ...state,
        pokemonListLoading: false,
        pokemonListLoadingFailed: false,
        data: action.payload,
      };
    case FETCH_POKEMON_LIST_ERROR:
      return {
        ...state,
        pokemonListLoading: false,
        pokemonListLoadingFailed: true,
      };
    default:
      throw new Error();
  }
};

export const fetchPokemonList = (initialUrl: string, initialData: []) => {
  const [url, setUrl] = useState(initialUrl);
  const [state, dispatch] = useReducer(dataFetchReducer, {
    pokemonListLoading: false,
    pokemonListLoadingFailed: false,
    data: initialData,
  });

  useEffect(() => {
    const fetchData = async () => {
      dispatch({ type: FETCH_POKEMON_LIST });
      try {
        const result = await axios(url);
        dispatch({ type: FETCH_POKEMON_LIST_SUCCESS, payload: result.data });
      } catch (error) {
        dispatch({ type: FETCH_POKEMON_LIST_ERROR });
      }
    };

    fetchData();
  }, [url]);

  return [state, setUrl];
};

和整个组件

import React, { FunctionComponent } from 'react';
import { fetchPokemonList, InitialState } from '../../hooks/fetchPokemonList';

const PokemonList: FunctionComponent = () => {
  const [{
      data: { results: pokemonList },
      pokemonListLoading,
      pokemonListLoadingFailed,
    },
  ] = fetchPokemonList('https://pokeapi.co/api/v2/pokemon',[]);

  return (
    <div>
      PokemonList
      {pokemonListLoading ? (
        <div>Laoding...</div>
      ) : (
      pokemonList && pokemonList.map((pokemon: { name: string}) => (
          <div key={pokemon.name}>{pokemon.name}</div>
        ))
      )}
      {pokemonListLoadingFailed && <div>Error</div>}
    </div>
  )
}

export { PokemonList }

Webstorm 显示的错误代码

TS2339:类型“{ pokemonListLoading: 上不存在属性“数据”: 布尔值; pokemonListLoadingFailed:布尔值;数据: []; } | { pokemonListLoading:布尔值; pokemonListLoadingFailed:布尔值;数据: 口袋妖怪列表; } |调度...>>'。

【问题讨论】:

    标签: reactjs typescript react-hooks use-effect


    【解决方案1】:

    问题在这一行:

    dispatch({ type: FETCH_POKEMON_LIST_SUCCESS, payload: result.data });
    

    在不使用新 data 值的密钥的情况下作为有效负载发送的位置。

    然后在代码部分中,您使用有效负载对象设置 data,这会导致您遇到错误:

    case FETCH_POKEMON_LIST_SUCCESS:
      return {
        ...state,
        pokemonListLoading: false,
        pokemonListLoadingFailed: false,
        data: action.payload,
      };
    

    尝试像这样传递您的有效负载:payload: { data: result.data }
    然后分别设置你的datadata: action.payload.data

    【讨论】:

    • 每次获取数据都可以,但我有这个奇怪的错误消息 TS2339: 属性 'pokemonListLoading' 在类型'{ pokemonListLoading: boolean; 上不存在; pokemonListLoadingFailed:布尔值;数据: []; } | { pokemonListLoading:布尔值; pokemonListLoadingFailed:布尔值;数据:口袋妖怪列表; } |调度...>>'。
    猜你喜欢
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2017-12-31
    • 2018-08-02
    • 2021-08-04
    • 1970-01-01
    相关资源
    最近更新 更多