简体   繁体   English

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

[英]Use custom middleware for async actions. Actions must be plain objects

I am new to this, and this error occurs, looked at a lot of solutions on it, nothing helped to fix.我对此很陌生,发生了这个错误,看了很多关于它的解决方案,没有任何帮助解决。

Another mistake in store.js. store.js 中的另一个错误。 When point to thunk.当指向thunk。 Argument type ThunkMiddleware & {withExtraArgument (extraArgument: E): ThunkMiddleware{}, AnyAction, E>} is not assignable to parameter type Function参数类型 ThunkMiddleware & {withExtraArgument (extraArgument: E): ThunkMiddleware{}, AnyAction, E>} 不可分配给参数类型 Function

Actions.js动作.js

export const SET_YEAR = 'SET_YEAR';
export const FETCH_USERS_EXAMPLE = "FETCH_USERS_EXAMPLE";

export function setYear(year) {
    return {
        type: 'SET_YEAR',
        payload: year,
    }
}

export async function getFreeData() {
    try {
        return async (dispatch) => {
            let res = await fetch(`https://jsonplaceholder.typicode.com/users`);
            let userList = await res.json();
            dispatch({
                type: "FETCH_USERS_EXAMPLE",
                payload: userList
            });
            return userList;
        }
    } catch (e) {
        console.log("Error", e);
    }
}

Reducer.js减速器.js

import {SET_YEAR, FETCH_USERS_EXAMPLE} from '../Actions/TestAction';

export function testReducer(state ={year: ''}, action) {
    switch (action.type) {
        case 'SET_YEAR':
            return {...state, year: action.payload};
        case 'FETCH_USERS_EXAMPLE':
            return {...state, userList: action.payload};
        default:
            return state
    }
}

Container.js容器.js

import TestComponent from "./TestComponent";
import {setYear, getFreeData} from "../../Redux/Actions/TestAction";
import {connect} from "react-redux";
import React from "react";


const mapStateToProps = (store) => ({
    items: store.user,
    userList: store.page.userList,
    year: store.page.year
});

const mapDispatchToProps = {
    setYear,
    getFreeData
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(TestComponent);

Store.js商店.js

import {createStore, applyMiddleware} from 'redux'
import {rootReducer} from './Reducers/rootReducer';
import thunk from 'redux-thunk';
import logger from "redux-logger";

export const store = createStore(rootReducer, applyMiddleware(thunk, logger));

webpack网络包

const path = require("path");

module.exports = {
  entry: ['babel-polyfill', "./src/index.js"],
  mode: "development",
  output: {
    filename: "./main.js"
  },
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    compress: true,
    port: 3000,
    watchContentBase: true,
    progress: true
  },

  devtool: "source-map",
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.html$/,
        use: ['raw-loader']
      },
      {
        test: /\.scss$/,
        use: [
          {
            loader: "style-loader"
          },
          {
            loader: "css-loader"
          },
          {
            loader: "sass-loader"
          }
      ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ["file-loader"]
      }
    ]
  }
};

Your getFreeData function should not be async or it will return a promise.你的getFreeData函数不应该是异步的,否则它会返回一个承诺。

Instead, return the function for redux-thunk:相反,返回 redux-thunk 的函数:

export function getFreeData() {
  return async (dispatch) => {
    try {
      let res = await fetch(`https://jsonplaceholder.typicode.com/users`);
      let userList = await res.json();
      dispatch({
        type: "FETCH_USERS_EXAMPLE",
        payload: userList
      });
      return userList;
    }
    catch (e) {
      console.log("Error", e);
    }
  }
}

Hope this helps.希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动作必须是普通对象。 使用自定义中间件进行异步操作。 通过使用中间件 - Actions must be plain objects. Use custom middleware for async actions. by using middleware Redux Mock Store给出'动作必须是普通对象。 使用自定义中间件进行异步操作。 - Redux Mock Store giving 'Actions must be plain objects. Use custom middleware for async actions.' 使用 Jest 模拟会导致“操作必须是普通对象。 使用自定义中间件进行异步操作。” - Using Jest mocks results in “Actions must be plain objects. Use custom middleware for async actions.” 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 Redux 工具包 - Error: Actions must be plain objects. Use custom middleware for async actions. Redux toolkit 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error 如何修复:错误:操作必须是普通对象。 使用自定义中间件进行异步操作。? - How to fix: Error: Actions must be plain objects. Use custom middleware for async actions.? 动作必须是普通对象。 使用自定义中间件进行异步操作。 React,Redux,Thunk - Actions must be plain objects. Use custom middleware for async actions. React, Redux, Thunk Error get'Uncaught Error:操作必须是纯对象。 使用自定义中间件执行异步操作。” 由redux-thunk - Error get 'Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.' by redux-thunk 错误:操作必须是普通对象。 使用自定义中间件进行异步操作。 我究竟做错了什么? - Error: Actions must be plain objects. Use custom middleware for async actions. What am I doing wrong? 无法超越“动作必须是简单的对象。 使用自定义中间件进行异步操作。”尝试遵循redux.js.org教程时出错 - Can't beat the “Actions must be plain objects. Use custom middleware for async actions.” error while trying to follow redux.js.org tutorial
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM