javascript - Redux-thunk: `dispatch is not a function`

标签 javascript redux redux-thunk apollo

因此,我遇到了返回上述错误的操作的问题(见附图),而不是按预期更新 redux 状态。我在这里忽略了什么?

actionCreators.js

export function userToken(token) {
  console.log('userToken has been fired');
  return (dispatch) => {
    dispatch({
      type: 'Graphcool_Token',
      payload: token
    });
  } 
}

App.js

....
// Root Query
const allPostsCommentsQuery = graphql(All_Posts_Comments_Query, {
  options: {
    cachePolicy: 'offline-critical', 
    fetchPolicy: 'cache-first',
  },
});

export const mapDispatchToProps = (dispatch) => {
  return bindActionCreators(actionCreators, dispatch);
}

export default compose(
  allPostsCommentsQuery,
  connect(mapDispatchToProps)
)(Main);

reducer

var tokenDetails = function(state, action) {

  if (state === undefined) {
    state = [];
  }

  switch (action.type) {
    case 'Graphcool_Token':
      const newState = [action.payload];
      return newState;
    default:
      return state;
  }
}

export default tokenDetails;

登录用户.js

  signinUser: function(emailID, passwordID) {

    const email = emailID;
    const password = passwordID;

    this.props.client.mutate({
      mutation: signinUser_Mutation,
      variables: {
        email,
        password,
      },
      options: {
        cachePolicy: 'offline-critical', 
        fetchPolicy: 'cache-first',
      },
    })
    .then(this.updateStateLoginDetails)
    .catch(this.handleSubmitError);
  },

  updateStateLoginDetails: function({data}) {
    this.props.userToken(data.signinUser.token);
  },

商店.js

import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, autoRehydrate} from 'redux-persist';
import { syncHistoryWithStore } from 'react-router-redux';
import { browserHistory } from 'react-router'
import thunk from 'redux-thunk';
import rootReducer from './reducers/index';
import client from './apolloClient';
import localForage from 'localforage';

const middlewares = [thunk, client.middleware()];

const enhancers = compose(
    applyMiddleware(...middlewares),
    (typeof window.__REDUX_DEVTOOLS_EXTENSION__ !== 'undefined' || process.env.NODE_ENV !== 'production') ? window.__REDUX_DEVTOOLS_EXTENSION__() : (f) => f,
    autoRehydrate(),
);

const store = createStore(
  rootReducer,
  {}, // initial state
  enhancers
);

// begin periodically persisting the store
persistStore(store, {storage: localForage});

export const history = syncHistoryWithStore(
  browserHistory, 
  store
);

if(module.hot) {
  module.hot.accept('./reducers/', () => {
    const nextRootReducer = require('./reducers/index').default;
    store.replaceReducer(nextRootReducer);
  });
}

export default store;

enter image description here

最佳答案

您应该传递给 connect 的第一个参数是 mapStateToProps,它是一个接收 state 和组件 props< 的函数.. 如果你不需要它,你应该在那里添加 null:

连接(null,mapDispatchToProps)(主要)

顺便说一句,一般来说,你不需要bindActionCreators..通常返回一个对象就足够了,比如:

const mapDispatchToProps = {
  someActionName,
  someOtherAction,
}

关于javascript - Redux-thunk: `dispatch is not a function`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44265007/

相关文章:

javascript - Eloquent Javascript 中的练习 4.4 -- startsWith() 和 .slice

javascript - 从 native Webview 获取网页的动态内容

javascript - 如何在 onClick 函数上更改存储在 Redux 上的 bool 值?

javascript - 使用 redux-thunk/redux-observable 和 redux 的回调

javascript - 调用网站并获取 JSON 信息

javascript - Bootstrap Accordion + x-editable

reactjs - 我可以在 reducer 中发送一个 Action 吗?

reactjs - Redux 待办事项列表高级

reactjs - 使用 thunk 中间件从非 React 组件调度异步 redux 操作

reactjs - 在 React Redux 中测试