[英]Faced Object literal may only specify known properties in React Typescript
I was creating project in React Typescript and decided to use Hooks + useContext + useReducer.我在 React Typescript 中创建项目并决定使用 Hooks + useContext + useReducer。 THen, I created seperate file to configure initial state and provider.
然后,我创建了单独的文件来配置初始 state 和提供程序。 But I face the error where
ADD_TRANSACTIONS
is used.但我面临使用
ADD_TRANSACTIONS
的错误。 Here is the code I have now:这是我现在拥有的代码:
import * as React from "react";
import { createContext, useReducer, ReactNode } from "react";
import transactionReducer from "./transactionReducer";
const initialState = {
transactions: [
{ id: 1, text: "Cash", amount: 10000 },
{ id: 2, text: "Food", amount: -10000 },
],
};
export const Context = createContext(initialState);
interface Props {
children: ReactNode;
}
const GlobalProvider = ({ children }: Props) => {
const [state, dispatch] = useReducer(transactionReducer, initialState);
const ADD_TRANSACTIONS = (transaction: any) => {
dispatch({ type: "ADD_TRANSACTIONS", payload: transaction });
};
return (
<Context.Provider
value={{
transactions: state.transactions,
ADD_TRANSACTIONS, Here I face the error which is defined below
}}
>
{children}
</Context.Provider>
);
};
export default GlobalProvider;
Here is that error:这是那个错误:
'{ transactions: any; ADD_TRANSACTIONS: (transaction: any) => void; }' is not assignable to type '{ transactions: { id: number; text: string; amount: number; }[]; }'.
Object literal may only specify known properties, and 'ADD_TRANSACTIONS' does not exist in type '{ transactions: {
id: number; text: string; amount: number; }[]; }'.
You made it so your context value (what's provided to components) is the same thing as the reducer initial state.您这样做是为了使您的上下文值(提供给组件的内容)与减速器初始 state 相同。 These are different things because
ADD_TRANSACTIONS
should exist in the context but not in the state.这些是不同的东西,因为
ADD_TRANSACTIONS
应该存在于上下文中,而不是 state 中。 Here's what I would recommend trying:这是我建议尝试的方法:
import * as React from "react";
import { createContext, useReducer, ReactNode } from "react";
import transactionReducer from "./transactionReducer";
interface Transaction {
id: number;
text: string;
amount: number;
}
interface State {
transactions: Transaction;
}
interface GlobalContextType {
addTransaction(transaction: Transaction): void;
state: State;
}
const initialState: State = {
transactions: [
{ id: 1, text: "Cash", amount: 10000 },
{ id: 2, text: "Food", amount: -10000 },
],
};
// Create context with no default value since it will be provided in the component.
// You could try to provide a default value, but it will never be fully complete
// with working functions and everything so I don't see the point in trying.
export const Context = createContext<GlobalContextType>(undefined as any);
// If you don't have any props, get rid of this and replace React.FC<Props>
// later with just React.FC
interface Props {
}
const GlobalProvider: React.FC<Props> = ({ children }) => {
const [state, dispatch] = useReducer(transactionReducer, initialState);
return (
<Context.Provider
value={{
state,
addTransaction(transaction) {
dispatch({ type: "ADD_TRANSACTIONS", payload: transaction });
}
}}
>
{children}
</Context.Provider>
);
};
export default GlobalProvider;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.