我正在编写一个自定义钩子来从 API 获取一些数据。如果可能,我希望返回的数据是类型安全的。这可以用泛型来完成吗?
type Action = { type: 'PENDING' } | { type: 'SUCCESS'; payload: any } | { type: 'FAIL' };
interface State {
isLoading: boolean;
isError: boolean;
data: any;
}
const dataFetchReducer = (state: State, action: Action): State => {
switch (action.type) {
case 'PENDING':
return {
...state,
isLoading: true,
};
case 'SUCCESS': {
return {
...state,
isLoading: false,
isError: false,
data: action.payload,
};
}
case 'FAIL':
return {
...state,
isLoading: false,
isError: true,
};
default:
throw new …Run Code Online (Sandbox Code Playgroud) 我尝试在 iOS Safari 和 iOS Chrome 上加载网络应用程序时收到错误。在桌面上进行私密浏览时没有错误。
我添加了以下内容:
if (window.indexedDB) {
console.log('IDB supported');
var db = new Dexie('Stir');
//Dexie stuff
}
Run Code Online (Sandbox Code Playgroud)
使用 Safari 开发工具,正在记录“支持 IDB” - 但随后会弹出错误/警报并阻碍操作。
我将所有 dexie 代码都封装在里面if (window.indexedDB)- 以确保它仅在 IDB 在浏览器中可用时才运行。
我的完整仓库位于https://github.com/georgecook92/Stir。如果查看这段代码,对 dexie 的调用位于React 的actionsindex.js和 main中。index.js
dexie ×1
generics ×1
indexeddb ×1
ios ×1
javascript ×1
react-hooks ×1
reactjs ×1
typescript ×1