【问题标题】:redux-promise use 'then()' after dispatching actionredux-promise 在调度动作后使用 'then()'
【发布时间】:2017-06-07 16:43:10
【问题描述】:

我想删除一个帖子。我使用 redux-promise 作为中间件。 我的操作如下所示:

export function deletePost(id) {
    const request = axios.delete(`${ROOT_URL}/${id}?apiKey=${API_KEY}`)

    return {
        type: DELETE_POST,
        payload: request
    }
}

然后我的组件中有一个按钮来触发操作。

onDeleteClick() {
        deletePost(id)
        .then(setState({ redirect: true }))
    }

问题是我不能使用'then()'。我只想在删除帖子后将用户重定向到主页。 请帮帮我。


应要求提供源代码。

actions/index.js

import axios from 'axios';

export const DELETE_POST = 'DELETE_POST';

export const ROOT_URL = 'example.com';
export const API_KEY = 'randomstring';

export function deletePost(id) {
    const request = axios.delete(`${ROOT_URL}/${id}?apiKey=${API_KEY}`)

    return {
        type: DELETE_POST,
        payload: request
    }
}

reducers/post_reducer.js

import {  DELETE_POST } from '../actions/index';

const INITIAL_STATE = { all: [], post: null };

export default function(state = INITIAL_STATE, action) {
    switch(action.type) {
            return state.all.filter(post => post !== action.payload.data);
        default:
            return state;
    }
}

组件/PostShow.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchPost, deletePost } from '../actions/index';
import { Link } from 'react-router-dom';
import { Redirect } from 'react-router';

class PostsShow extends Component {
     constructor(props) {
        super(props)

        this.state = {
            redirect: false
        }
    }
    componentWillMount() {
        this.props.fetchPost(this.props.match.params.id)
    }

    onDeleteClick() {
        deletePost(this.props.match.params.id)
        .then(() => this.setState({ redirect: true }))
    }


    render() {

        if(!this.props.post) {
            return <div>Loading...</div>
        }
        if(this.state.redirect) {
            return <Redirect to='/'/>
        }

        return (

            <div className='blog-post container'>
                <h3>{this.props.post.title}</h3>
                <h6>Categories: {this.props.post.categories}</h6>
                <p>{this.props.post.content}</p>
                <Link to='/'><button className='btn btn-primary'>Back</button></Link>
                { this.props.user
                ? <button className='btn btn-danger' onClick={this.onDeleteClick.bind(this)}>Delete</button> 
               : null }
            </div>
        );
    }
}

function mapStateToProps(state) {
    return {
        post: state.posts.post,
        user: state.user
    }
}

export default connect(mapStateToProps, { fetchPost, deletePost }) (PostsShow)

【问题讨论】:

  • 为什么不能使用thenrequest 承诺对吗?因此deletePost(id).request.then(...) 应该可以工作。

标签: reactjs redux


【解决方案1】:

根据redux-promise source code,看起来它应该返回链式承诺:

return isPromise(action.payload)
  ? action.payload.then(
      result => dispatch({ ...action, payload: result }),
      error => {
        dispatch({ ...action, payload: error, error: true });
        return Promise.reject(error);
      }
    )
  : next(action);

所以,我假设您可以取消调度。

也就是说,您的 sn-p 有几个潜在问题。如果它在一个组件中,那么deletePost 来自哪里?您也没有在setState 前面使用this。假设deletePost 是一个绑定动作创建者,这个例子应该是正确的:

onDeleteClick() {
    this.props.deletePost(id)
        .then(() => this.setState({redirect : true});
}

【讨论】:

  • 我的 deletePost(id) 在 actions/index.js 我试过你的 onDeleteClick() 版本,我有一个错误“.then() is not a function”
  • deletePost 是否实际上已绑定,以便在调用时调度?你能发布更多你的代码吗?
  • 当我从 onDeleteClick 函数中删除带有“.then()”的代码行时,一切正常。操作有效,帖子已删除。我读到只有 Redux-thunk 让我们控制 Redux 的 dispatch() 方法,该方法负责将每个触发的操作发送到我们所有的 reducer。这允许我们以异步方式从动作创建者内部调用动作。我想我必须使用 Redux-thunk。
  • 否——任何中间件都可以修改dispatch的返回值。对于redux-thunk,它返回thunk 函数的结果。基于redux-promise 来源,我希望从调度中返回承诺。只是为了好玩,尝试做const result = this.props.deletePost(id); console.log(result);。我仍然不确定你实际上是在调度 deletePost 方法 - 听起来你只是在调用它而不调度。
  • 我上传了更多代码。我不知道你打电话不调度是什么意思。我的意思是我调用动作创建者,而不是动作创建者调用减速器,减速器更新状态。我是这样理解的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2020-04-26
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
相关资源
最近更新 更多