简体   繁体   English

Redux:dispatch(...).then 不是函数

[英]Redux: dispatch(…).then is not a function

I have such action :我有这样的action

import { GET, POST, PUT, REMOVE } from "../../Utils/Http";

export const FETCH_ARTICLES = "FETCH_ARTICLES";
export const FETCH_ARTICLES_SUCCESS = "FETCH_ARTICLES_SUCCESS";
export const FETCH_ARTICLES_FAILURE = "FETCH_ARTICLES_FAILURE";
export const RESET_ARTICLES = "RESET_ARTICLES";


export function fetchArticles() {
  const request = GET("/articles");

  return {
    type: FETCH_ARTICLES,
    payload: request
  };
}

export function fetchArticlesSuccess(articles) {
  return {
    type: FETCH_ARTICLES_SUCCESS,
    payload: articles
  };
}

export function fetchArticlesFailure(error) {
  return {
    type: FETCH_ARTICLES_FAILURE,
    payload: error
  };
}

and reducer :reducer

import {
  FETCH_ARTICLES,
  FETCH_ARTICLES_SUCCESS,
  FETCH_ARTICLES_FAILURE,
  RESET_ARTICLES
} from "../Actions/Article";

const INITIAL_STATE = {
  articlesList: {
    articles: { data: [], total: 0 },
    error: null,
    loading: false
  },
  newTractor: { article: null, error: null, loading: false },
  activeTractor: { article: null, error: null, loading: false },
  deletedTractor: { article: null, error: null, loading: false }
};

const reducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case FETCH_ARTICLES:
      return {
        ...state,
        articleList: { articles: {}, error: null, loading: true }
      };
    case FETCH_ARTICLES_SUCCESS:
      return {
        ...state,
        articleList: { articles: action.payload, error: null, loading: false }
      };
    case FETCH_ARTICLES_FAILURE:
      return {
        ...state,
        articleList: { articles: {}, error: action.payload, loading: false }
      };
    case RESET_ARTICLES:
      return {
        ...state,
        articleList: { articles: {}, error: null, loading: false }
      };

    default:
      return state;
  }
};

export default reducer;

And i try it to use this way in list component :我尝试在list component使用这种方式:

import React, { Component } from "react";
import { connect } from "react-redux";
import { isUndefined } from "lodash";
import {
  fetchArticles,
  fetchArticlesSuccess,
  fetchArticlesFailure
} from "../../Store/Actions/Article";

class ArticleList extends Component {
  componentDidMount() {
    this.props.fetchArticles();
  }

  render() {
    return <div className="ui segment" />;
  }
}

const mapDispatchToProps = dispatch => {
  return {
    fetchArticles: () => {
      dispatch(fetchArticles()).then(response => {
        !response.error
          ? dispatch(fetchArticlesSuccess(response.payload.data))
          : dispatch(fetchArticlesFailure(response.payload.data));
      });
    }
  };
};

export default connect(null, mapDispatchToProps)(ArticleList);

also Http.js :还有Http.js

import axios from "axios";

const http = axios.create({
  baseURL: process.env.BASE_API_URL
});

export const GET = (url, params) => {
  return new Promise((resolve, reject) => {
    http({
      method: "get",
      url,
      params
    })
      .then(response => {
        resolve(response);
      })
      .catch(err => {
        console.log("GET err ", err);
        reject(err);
      });
  });
};

...

But as result I get:但结果我得到:

TypeError: dispatch is not a function in dispatch(fetchArticles()).then(response => { TypeError: dispatch is not a function dispatch(fetchArticles()).then(response => {

What I do wrong?我做错了什么?

Also how can i write this part:另外我该如何写这部分:

  fetchTractors()).then(response => {
    !response.error
      ? dispatch(fetchTractorsSuccess(response.payload.data))
      : dispatch(fetchTractorsFailure(response.payload.data));
  }

in component class?在组件类中? is it possible?是否可以? (not to move it to the mapDispatchToProps block) (不要将其移动到mapDispatchToProps块)

i took some ideas from here: https://github.com/rajaraodv/react-redux-blog/我从这里得到了一些想法: https : //github.com/rajaraodv/react-redux-blog/

The probles is here:问题在这里:

export default connect(mapDispatchToProps)(ArticleList);

First parameter should be mapStateToProps .第一个参数应该是mapStateToProps But you actually can pass null:但你实际上可以传递null:

export default connect(null, mapDispatchToProps)(ArticleList);

I can see many problems here:我可以在这里看到很多问题:

const mapDispatchToProps = dispatch => {
  return {
    fetchArticles: () => {
      dispatch(fetchArticles()).then(response => {
        !response.error
          ? dispatch(fetchArticlesSuccess(response.payload.data))
          : dispatch(fetchArticlesFailure(response.payload.data));
      });
    }
  };
};
  1. dispatch is a synchronous thing by default unless you have configured some middleware such as redux-thunk to handle functions. dispatch 默认是同步的,除非你配置了一些中间件,比如 redux-thunk 来处理函数。 dispatch takes native object as an argument in normal scenario. dispatch 在正常情况下将本机对象作为参数。

  2. dispatch does not return a promise. dispatch 不返回 promise。 So then can not be used,那么就不能用了,

  3. connect takes first arguments as mapStateToProps and second argument as mapDispatchtoProps. connect 将第一个参数作为 mapStateToProps,第二个参数作为 mapDispatchtoProps。 There is also third argument which is not generally used.还有第三个参数,通常不使用。 So I will not mention it for now.所以我暂时不提。

4.you need to pass the actions creators through mapDispatchToProps like this: 4.你需要像这样通过 mapDispatchToProps 传递动作创建者:

import { bindActionCreators } from "redux"
const mapDispatchToProps = dispatch => bindActionCreators({
 fetchArticles,
 fetchArticlesSuccess,
 fetchArticlesFailure,
}, dispatch)

如果有人在使用ts + redux时遇到这个问题,IDE 提示你没有then方法,你可以参考这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM