javascript - Uncaught Error : Actions must be plain objects

标签 javascript reactjs redux axios redux-thunk

我已经遍历了所有带有此标题的问题,但找不到可以防止此错误的解决方案。到目前为止我已经尝试过的是确保我不会混淆新旧 redux api,我正在使用 babel 进行转译,所以没有适用的 typescript 解决方案,我已经确保我在相关操作中返回了一个函数,我已经注释掉我的导入或 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;
};

Action :

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));
    });
};

调度 Action 的代码:

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(这是一门类(class))所以我真的可能遗漏了一些明显的东西。如果我遗漏了一些相关的内容,请告诉我。谢谢

最佳答案

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

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

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

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

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

关于javascript - Uncaught Error : Actions must be plain objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46675997/

相关文章:

reactjs - 当用户点击后退按钮时 React Native 刷新内容 - 使用 Hooks

javascript - React-native-ui-lib 是否可用于 React Native 0.60? (安卓X)

javascript - 如何在任何调度操作上运行 redux-saga 中间件?

javascript - Redux-thunk 派发一个 Action 并等待重新渲染

javascript - Angular JS 和 Bootstrap Modal 的数据绑定(bind)问题

javascript - 我在向 CSS 中的文本框添加文本时遇到问题

javascript - react : Big Calendar Font Style

javascript - 如何在 React/Next.js 中更新状态而不进入无限重新渲染循环?

javascript - 从表单中获取值并插入到span中

javascript - 在数组的负索引中添加元素?