Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使store.dispatch()在action.js文件中可用,而不是作为道具?_Reactjs_Redux_React Redux - Fatal编程技术网

Reactjs 如何使store.dispatch()在action.js文件中可用,而不是作为道具?

Reactjs 如何使store.dispatch()在action.js文件中可用,而不是作为道具?,reactjs,redux,react-redux,Reactjs,Redux,React Redux,目前,我正在使用如下连接从我的redux感知登录组件发送操作,该连接工作正常: import LoginActions from '../actions'; @connect(store() => store); ... this.props.dispatch(LoginActions.fetchingUser(formData)); fetchingUser操作创建者调用如下API: import API from '../utils/api'; ... function fetchi

目前,我正在使用如下连接从我的redux感知登录组件发送操作,该连接工作正常:

import LoginActions from '../actions';
@connect(store() => store);
...
this.props.dispatch(LoginActions.fetchingUser(formData));
fetchingUser操作创建者调用如下API:

import API from '../utils/api';
...
function fetchingUser(data) {
  API.getUser(data)
  return {
    type: FETCHING_USER
}
api.js文件的内部如下所示:

getUser: (data) => {
  request.post('something.com', data)
    .end((err, res) => {
      err ? console.log(err) : console.log(res)
  })
}
现在,我想要的是能够基于LoginActions.js文件内的API调用响应或直接从API.js文件分派操作,为此,分派需要在这些位置中的任何一个位置可用

应用程序文件夹结构如下所示,可能与此无关:

├── actions
|   └── LoginActions.js
├── components
|   └── Login.jsx
├── reducer
|   └── loginReducer.js
├── utils
    └── api.js
您可以使用中间件,并且可以访问thunk操作:

function fetchingUser(data) {
  return (dispatch, getState) => {
   API.getUser(data)
   .then(response => {
        if(response.condition){
            dispatch({type: YOUR_TYPE});
        }
   })
  };
}
不要忘记将中间件添加到商店配置中:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
    const store = createStore(
      rootReducer,
      applyMiddleware(thunk)
    );

我发现无法读取未定义的属性“then”时出错。API.getUser应返回一个承诺。找出请求未返回承诺的原因,问题得到解决,谢谢…是的,您没有返回任何内容,只是执行了控制台。日志: