【问题标题】:React, Typescript, Hooks - Property 'state' does not exist on typeReact,Typescript,Hooks - 类型上不存在属性“状态”
【发布时间】:2019-12-07 10:56:11
【问题描述】:

我正在尝试使用 React、typescript 和钩子 useContext 和 useReducer 创建一个简单的待办事项应用程序。

index.tsx

import React, { useContext, useReducer } from "react";
import ReactDOM from "react-dom";
import "./index.css";

import TodosContext from "./context";
import todosReducer from "./reducer";
import TodoList from "./components/TodoList";
import { ITodo } from "./context";

const App = () => {
  const initialState = useContext(TodosContext);
  const [state, dispatch] = useReducer(todosReducer, initialState);

  return (
    <TodosContext.Provider value={{ state, dispatch }}> <!-- error here --->
      <TodoList />
    </TodosContext.Provider>
  );
};

ReactDOM.render(
  <App />,

  document.getElementById("root")
);

这里我得到状态错误

Type '{ state: any; dispatch: Dispatch<any>; }' is not assignable to type '{ todos: { id: number; text: string; complete: boolean; }[]; }'.
  Object literal may only specify known properties, and 'state' does not exist in type '{ todos: { id: number; text: string; complete: boolean; }[]; }'.ts(2322)

TodoList.tsx

import React, { useContext } from "react";
import TodosContext from "../context";
import { ITodo } from "../context";
import { initialState } from "../context";

export default function TodoList() {
  const { state } = useContext(TodosContext); <!-- error here --->

  return (
    <div>
      <ul>
        {state.todos.map(todo => (
          <li key={todo.id}>
            <span>{todo.text}</span>
            <button>edit</button>
            <button>delete</button>
          </li>
        ))}
      </ul>
    </div>
  );
}

在这里我得到了同样的状态错误

Property 'state' does not exist on type '{ todos: { id: number; text: string; complete: boolean; }[]; }'.ts(2339)

context.tsx

import React from "react";

export interface ITodo {
  id: number;
  text: string;
  complete: boolean;
  state?: any; <!-- state is included in the interface --->
}

export const initialState = [
  { id: 1, text: "todo 1", complete: false },
  { id: 2, text: "todo 2", complete: false },
  { id: 3, text: "todo 3", complete: true }
];

const TodosContext = React.createContext({
  todos: [
    { id: 1, text: "todo 1", complete: false },
    { id: 2, text: "todo 2", complete: false },
    { id: 3, text: "todo 3", complete: true }
  ]
});

export default TodosContext;

reducer.tsx

export default function reducer(state: any, action: any) {
  switch (action.type) {
    default:
      return state;
  }
}

我已向 ITodo 界面添加状态以查看是否有帮助,但没有。

如何为类型添加状态

【问题讨论】:

    标签: reactjs typescript react-hooks


    【解决方案1】:

    有两个错误。它们是不同类型的错误。第一期:

    const { state } = useContext(TodosContext);
    

    TodosContext 已使用某种类型创建,并在调用 useContext 后返回该类型的对象。使用显式类型来更好地理解它的工作原理:

    interface ITodosContextData {
      todos: ITodo[];
    }
    const TodosContext = React.createContext<ITodosContextData>(initialState);
    const state: ITodosContextData = useContext(TodosContext);
    

    您在ITodosContextData 上使用扩展运算符{ key1, key2, key3 }ITodosContextData 没有成员 state。你真正想要的是这样的:

    const state = useContext(TodosContext);
    

    第二期类似TodosContext.Provider.valueITodosContextData 类型。而且您正在提供一些不同类型的临时数据对象。

    用途:

    const [state, dispatch] = useReducer(todosReducer, initialState);
    <TodosContext.Provider value={state}>
    

    解决您的问题。

    【讨论】:

    • 我仍然收到initialState 在这一行的错误const TodosContext = React.createContext&lt;ITodosContextData&gt;(initialState); 错误显示Argument of type '{ id: number; text: string; complete: boolean; }[]' is not assignable to parameter of type 'ITodosContextData'.
    • 在您的原始示例中,您的initialState 变量是ITodo[],但您在传递给createContext 时使用的是{todos: ITodo[]}。假设这两者具有相同的类型。你的没有。
    猜你喜欢
    • 2023-01-09
    • 2020-09-10
    • 2021-03-06
    • 2020-04-23
    • 2020-05-25
    • 2020-05-15
    • 1970-01-01
    • 2020-01-16
    • 2019-08-18
    相关资源
    最近更新 更多