[英]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 */));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.