rematch / core操作必须是普通对象。使用自定义中间件进行异步操作

时间:2019-01-22 09:19:31

标签: reactjs redux react-redux

我使用@ rematch / core并遇到问题。 在本地,开发错误时不存在此类问题,但是当我将其倒出时,出现错误,因此无法调试。也许有人碰到过这个?

const model = {
  state: initialState,

  reducers: {
    startLoading(state: IDataState, event: string | number) {
      return { ...state, loaders: { ...state.loaders, [event]: true } };
    },

    stopLoading(state: IDataState, event: string | number) {
      return { ...state, loaders: { ...state.loaders, [event]: false } };
    },

    saveData(state: IDataState, data: any[]) {
      return { ...state, data };
    },
  },

  effects: {
    async getData() {
      startLoading('getData');
      try {
        const { data } = await request({
          url: `URL`,
          method: 'GET',
        });
        dispatch({
          type: 'data/saveData',
          payload: data,
        });
      } catch (error) {
        console.log(error.message);
      } finally {
        stopLoading('getData');
      }
    },
  },
};

export default model;

store.ts

import { init } from '@rematch/core';

// Models
import data from './models/data.model';

const store = init({
  models: {
    data,
  },
});

export default store;

index.ts

import HomePage from './HomePage';
import { connect } from 'react-redux';

interface ISubscriptions {
  games: { [key: string]: any };
}

export default connect(
  ({ data }: ISubscriptions) => ({
    data,
  }),
  null,
)(HomePage);

并使用此

this.props.dispatch({ type: 'data/getData' });

任何地方都可以被{type:'xxx',有效载荷:data}形式的对象调用

---更新---

如果删除此代码(webpack),问题将消失。

optimization: {
    minimizer: [
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false,
            beautify: false,
          },
        },
      }),
    ],
  },

0 个答案:

没有答案