小编Geo*_*ook的帖子

useReducer 中返回参数的通用类型

我正在编写一个自定义钩子来从 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)

generics typescript reactjs react-hooks

6
推荐指数
1
解决办法
3569
查看次数

Dexie/IndexedDB 移动私密浏览错误 - 呃哦:缺少 API 错误:IndexedDB API 不可用

我尝试在 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

javascript incognito-mode ios indexeddb dexie

3
推荐指数
1
解决办法
2427
查看次数