Uncaught Error: Actions must be plain objects

Uncaught Error: Actions must be plain objects

我已经遍历了所有带有此标题的问题,但找不到防止此错误的解决方案。到目前为止我已经尝试过的是确保我不会混淆新旧 redux api,我正在使用 babel 进行转译,所以没有适用的打字稿解决方案,我已经确保我在操作中返回了一个函数有问题,我已经注释掉了我的导入或 thunk 以确保它中断,并且我在导入它之后注销了 thunk 并且我得到了一个功能,并且我已经从 depricated 版本更新了 devtools 扩展。没有成功。任何帮助深表感谢。相关代码如下:

商店:

const redux = require('redux');
const {combineReducers, createStore, compose, applyMiddleware} = require('redux');
import {default as thunk} from 'redux-thunk';

const {nameReducer, hobbyReducer, movieReducer, mapReducer} = require('./../reducers/index');

export const configure = () => {

    const reducer = combineReducers({
        name: nameReducer,
        hobbies: hobbyReducer,
        movies: movieReducer,
        map: mapReducer
    });

    const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

    const store = createStore(reducer, composeEnhancers(applyMiddleware(thunk)));   

    return store;
};

操作:

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url};

export let fetchLocation = () => (dispatch, getState) => {
    dispatch(startLocationFetch());

    axios.get('http://ipinfo.io').then(function(res) {
        let loc = res.data.loc;
        let baseURL = 'http://maps.google.com?q=';

        dispatch(completeLocationFetch(baseURL + loc));
    });
};

调度动作的代码:

console.log('starting redux example');

const actions = require('./actions/index');
const store = require('./store/configureStore').configure();

let unsubscribe = store.subscribe(() => {
    let state = store.getState();

    if(state.map.isFetching){
        document.getElementById('app').innerHTML = 'Loading...';
    } else if(state.map.url){
        document.getElementById('app').innerHTML = '<a target=_blank href = "' + state.map.url + '">Location</a>';
    }
});



store.dispatch(actions.fetchLocation());

我现在正在学习 React/Redux(这是一门课程)所以我真的可能遗漏了一些明显的东西。如果我遗漏了一些相关的内容,请告诉我。谢谢

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

不是 return 对象 ,而是应该导致语法错误

要直接从箭头函数return一个对象,需要设置括号,否则会被解释为一个块:

export let startLocationFetch = () => ({type: 'START_LOCATION_FETCH'});

编辑:感谢 Nicholas 指出确实不是语法错误。

export let startLocationFetch = () => {type: 'START_LOCATION_FETCH'};

export let completeLocationFetch = (url) => {type: 'COMPLETE_LOCATION_FETCH', url}

我认为这些行是问题所在。函数的简短语法很好用,但是对于返回一个对象,你应该用括号把它包起来,像这样:

export let startLocationFetch = () => ({type: 'START_LOCATION_FETCH'});

export let completeLocationFetch = (url) => ({type: 'COMPLETE_LOCATION_FETCH', url})

所以转译器知道接下来是必须返回的单个参数,而不是函数体。