我是 TS 的新手,正在尝试更新我从前员工那里继承的代码。我知道它与 TS 相关,并且尝试了不同类型但无法解决它。任何帮助都会很棒。提前致谢。
这是代码:
import React from 'react';
export interface HistoryType {
history?: number;
setHistory: (value: number) => void;
}
const HistoryContext = React.createContext<HistoryType | undefined>(undefined);
export const HistoryProvider: React.FC = ({ children }) => {
const [history, setHistory] = React.useState();
return (
<HistoryContext.Provider value={{ history, setHistory}}>
{children}
</HistoryContext.Provider>
);
};
export const useHistoryState = () => {
const context = React.useContext(HistoryContext);
if (context === undefined) {
throw new Error('useHistoryState error');
}
return context;
};
错误截图:
https://i.stack.imgur.com/YKbri.png
ibeautiful
相关分类