简体   繁体   English

REACT-REDUX:未捕获错误:操作必须是普通对象

[英]REACT-REDUX : Uncaught Error: Actions must be plain objects

Below is the code to fetch movies using React-Redux.下面是使用 React-Redux 获取电影的代码。 I have used fetch() to fetch thedata from API, but i got the error as "Uncaught Error: Actions must be plain objects. Use custom middleware for async actions."我使用 fetch() 从 API 获取数据,但我收到错误消息“未捕获的错误:操作必须是普通对象。使用自定义中间件进行异步操作。”

HomeAction.js HomeAction.js

import {API_URL,  API_KEY} from '../config'; 

export function getMovies(movies)
{
    return function(dispatch){
    const url =`${API_URL}movie/popular?api_key=${API_KEY}&language=en-us&page=1`; 
    fetch(url)
      .then(response => response.json())  
      .then(function(data){
        dispatch({type:"GET_MOVIES", payload:data.results})
      })
      .catch(function(err){
        dispatch({type:"GET_MOVIES", payload:err})
      })
    }  
}

HomeReducer.js HomeReducer.js

export function HomeReducer(state ={
            },action)
{
    console.log(action);
    switch(action.type)
    {
        case "GET_MOVIES" : 
           return {...state,...action.payload};
           break;
       default : 
       break;
    }
    return state;
}

app.js应用程序.js

import React from 'react';
import ReactDOM from 'react-dom';
//redux 
import {createStore, applyMiddleware} from 'redux';
import {createLogger} from "redux-logger";
//to connect to react and redux
import {Provider} from 'react-redux';

//STEP 3
import reducers from './Reducers/indexReducer';


import App from './App';

const middleware = applyMiddleware(createLogger());

//STEP 1
const store = createStore(reducers,middleware);

ReactDOM.render(
   <Provider store={store}>     
    <App />
    </Provider> 
    ,
  document.getElementById('root')
);

So i have tried to use axios by browing and tried to use redux-thunk but i got the same error.所以我试图通过浏览来使用 axios 并尝试使用 redux-thunk 但我得到了同样的错误。 Please help me to resolve this issue请帮我解决这个问题

When you are using redux you have to return a plain js object see this guide enter link description here当您使用 redux 时,您必须返回一个普通的 js object 请参阅本指南在此处输入链接描述

for ex:例如:

{
   type: ADD_TODO,
   text: 'Build my first Redux app'
}

in your case to perform a async action you simply use a middleware see this在您的情况下执行异步操作,您只需使用中间件,请参阅此

If you want to use actions as indicated by you (async actions), then you need to use the redux-thunk package.如果您想使用您指定的操作(异步操作),那么您需要使用redux-thunk package。 To do this, first install it:为此,首先安装它:

npm install --save redux-thunk

Next, connect it as middleware to your store:接下来,将其作为中间件连接到您的商店:

import React from 'react';
import ReactDOM from 'react-dom';
import thunk from 'redux-thunk';
import { createStore, applyMiddleware } from 'redux';
import { createLogger } from "redux-logger";
import { Provider } from 'react-redux';

import reducers from './Reducers/indexReducer';

import App from './App';

const middleware = applyMiddleware(createLogger(), thunk);

const store = createStore(reducers, middleware);

ReactDOM.render(
  <Provider store={store}>     
    <App />
  </Provider>,
  document.getElementById('root')
);

After that, you can use your event as you would like, that is:之后,您可以随意使用您的事件,即:

store.dispatch(getMovies(/* args */));

You can learn more about this here.您可以在此处了解更多信息。

And here you can see an example. 在这里你可以看到一个例子。

暂无
暂无

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

相关问题 未捕获的错误:操作必须是普通对象(React/Redux) - Uncaught Error: Actions must be plain objects (React/Redux) 错误:动作必须是普通对象。 使用自定义中间件进行异步操作。 React-redux错误 - Error: Actions must be plain objects. Use custom middleware for async actions. React-redux error React-Redux:动作必须是普通对象。 使用自定义中间件进行异步操作错误 - React-Redux: Actions must be plain objects. Use custom middleware for async actions Error react-redux 错误:操作必须是普通对象。 使用自定义中间件进行异步操作 - react-redux Error: Actions must be plain objects. Use custom middleware for async actions Actions 必须是普通对象 Error In React 和 Redux - Actions must be plain objects Error In React and Redux 动作必须是普通对象。 相反,实际类型是:&#39;undefined&#39;。 React-Redux - Actions must be plain objects. Instead, the actual type was: 'undefined'. React-Redux React - Native &#39; Redux Uncaught Error: Actions 必须是普通对象。 按下按钮时使用自定义中间件进行异步操作 - React - Native ' Redux Uncaught Error: Actions must be plain objects. Use custom middleware for async actions' on button press 响应redux调度程序“操作必须是普通对象”错误 - react redux dispatcher “Actions must be plain objects” error 动作必须是React / Redux中的普通对象? - Actions must be plain Objects in React/Redux? 未捕获的错误:动作必须是普通对象? - Uncaught Error: Actions must be plain objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM