javascript - TypeScript React 上下文 API
问题描述
我创建了一个用户上下文 API(此处的代码:https ://pastebin.pl/view/e0b5e0f4 ),然后我可以像这样阅读它:
const { state: { user }, dispatch} = useUserContext();
但我想这样做(数组而不是对象):
const [{ user }, dispatch] = useUserContext();
所以我将 UserProvider 更改为:
export const UserProvider: React.FC<UserProviderProps> = ({
reducer,
initialState,
children,
}) => (
<UserContext.Provider value={useReducer(reducer, initialState)}>
{children}
</UserContext.Provider>
);
我收到提示错误消息:
Type '[UserProps, Dispatch<UserActions>]' is not assignable to type 'UserContextProps'.ts(2322)
index.d.ts(337, 9): The expected type comes from property 'value' which is declared here on type 'IntrinsicAttributes & ProviderProps<UserContextProps>'
我对 TypeScript 有点陌生,我被困在这里
解决方案
推荐阅读
- android - Flutter - 从 base64 创建 Blob
- java - 在 Mongo 实体类上覆盖 hashCode() 和 equals() 是否安全
- angular - 多个 ag-grid 表导致浏览器挂起
- arrays - 如何根据雇用日期计算员工的 ID 号?
- oracle - PL-SQL 循环遍历值以获取动态查询和存储结果
- java - 如何在 bean 类中使用 groupBy
- c# - 与 gRPC 的进程内通信
- firebase - “ task.future.then ” 在我的 Flutter 代码中不断抛出错误
- css - 在多列中组织列表项
- javascript - 如何测试使用 props.history 的按钮 onClick 在 rtl 中的功能?