Reactjs Redux Thunk Action=组件willmount中未定义

Reactjs Redux Thunk Action=组件willmount中未定义,reactjs,redux,Reactjs,Redux,我正在尝试从componentWillMount分派一个函数。绑定动作创建者函数fetchDraftAdventures在componentWillMount外部工作,但不在内部工作 componentWillMount() { let {draftAdventures, editableAdventures} = this.props; console.log("this props") this.props.fetchDraftAdventures(1); c

我正在尝试从componentWillMount分派一个函数。绑定动作创建者函数fetchDraftAdventures在componentWillMount外部工作,但不在内部工作

componentWillMount() {
    let {draftAdventures, editableAdventures} = this.props;
    console.log("this props")
    this.props.fetchDraftAdventures(1);
    console.log(' adventures props', this.props)
  }
应用程序连接

export default withRouter(connect(
  state => {
    return {
      editableAdventures: state.editableAdventures,
      draftAdventures: state.draftAdventures,
    };
  },
  dispatch => ({
    fetchAdventureEditable: bindActionCreators(fetchAdventureEditable, dispatch),
    fetchEditableAdventures: bindActionCreators(fetchEditableAdventures, dispatch),
    fetchAdventureDraft: bindActionCreators(fetchAdventureDraft, dispatch),
    fetchDraftAdventures: bindActionCreators(fetchDraftAdventures, dispatch),
    dispatch
  })
)(AdventuresDashboard));
以下是行动本身:

// Constants
import {
  GET_DRAFT_ADVENTURES_REQUESTED,
    GET_DRAFT_ADVENTURES_SUCCEEDED,
    GET_DRAFT_ADVENTURES_REJECTED
} from '../../../constants/Actions'

import {get} from '../../../lib/request';

const DraftAdventures = (args) => {
  console.log('draft adventures', args)
  const Authorization = localStorage.getItem('authToken');

  const pageNumber = args;
  const lastIndex = pageNumber.length - 1;
  let pageParam = '';

  if (pageNumber[lastIndex]) {
    pageParam = `?page=${pageNumber[lastIndex]}`

    return dispatch => {

      dispatch({ type: GET_DRAFT_ADVENTURES_REQUESTED, payload: `Fetching Draft Adventures` })
      get(`/draft-adventures/${pageParam}&ordering=-updated_at`, {},
      {Authorization})
      .then(response => {
        if (response) {
          if (response.data) {
            let page = null;
            if (response.data.next !== null) {
              page = response.data.next.split('page=')[1];
              page = Number(page);
            }
            dispatch({ type: GET_DRAFT_ADVENTURES_SUCCEEDED, payload: response.data, pageNumber: Number(page)})

          } else {
            dispatch({ type: GET_DRAFT_ADVENTURES_REJECTED, payload: 'NULL response.data' })
          }
        } else {
          dispatch({ type: GET_DRAFT_ADVENTURES_REJECTED, payload: 'NULL response' })
        }
      }).catch(error => {
        console.log('errorer', error)
        dispatch({ type: GET_DRAFT_ADVENTURES_REJECTED, payload: error })
      })

    }
  }
};

export default (DraftAdventures);
对此的响应是以下堆栈跟踪:

据我所知,从thunk中间件传递到路由器中间件的操作是未定义的。在我的fetchDraftAdventure函数中,
dispatch({type:GET\u DRAFT\u ADVENTURES\u REQUESTED,payload:'Fetching DRAFT ADVENTURES'})
从未被调用。为什么不呢

另外,我的商店配置文件:

// Libraries
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import {logger} from 'redux-logger';

import createHistory from 'history/createBrowserHistory'
import { routerMiddleware } from 'react-router-redux'

// Components
import DevTools from '../devTools/DevTools';

// Reducers
import reducer from '../reducers';

// Config
import reduxLoggerConfig from '../config/reduxLoggerConfig';

//const logger = createLogger(reduxLoggerConfig);

const history = createHistory()
const middlewareRouter = routerMiddleware(history)


const createDevStoreWithMiddleware = compose(
  applyMiddleware(thunk, middlewareRouter, logger),
  DevTools.instrument()
)(createStore);

console.log('createDev store', createDevStoreWithMiddleware(reducer))
export default function configureStore() {

  var store = createDevStoreWithMiddleware(reducer);

  // enable webpack hot module replacement for reducers
  if (module.hot && process.env.BUILD_TARGET === 'local') {
    module.hot.accept('../reducers/index.js', () => {
      const nextRootReducer = require('../reducers');
      store.replaceReducer(nextRootReducer);
    });
  }

  return store;
}

我想问题在于你的“砰”声

这不是
thunk
签名,您没有返回函数

更改为:

const DraftAdventures = (args) => ({dispatch}) => {...  
redux thunk
检查对象是否为
typeof
函数


您的返回在
if
语句中。

如果此条件不成立:
如果(pageNumber[lastIndex])
,则您的操作创建者
DraftAdventures
不会返回任何内容(例如,它返回
未定义的
。Redux尝试调度此未定义的操作,您会得到错误

动作创建者(
DraftAdventures
在本例中)需要始终返回一个动作。该动作可以是一个简单的对象,也可以是一个thunk(又称函数),或者是您安装了redux中间件来处理的其他动作


你的action creator有时返回thunk(好!),有时返回undefined(坏!)

伙计,我完全忘了我应该传入一个数组参数,我的if语句失败了,因为我传入的是
1
,而不是
[1]
。你指的是if语句完全修复了它。非常感谢!