redux react ajax,使用react-redux触发事件操作

我正在使用react / redux / react-redux来实现执行ajax请求的模式形式 .

如果我是正确的,react-redux使您能够:

将redux商店中的数据显示给您的组件

将容器中的事件调度到redux

我也使用redux-saga来处理redux触发的Ajax请求 .

但是,当从Redux触发特定事件时,我不知道如何触发操作(例如:关闭模式窗体) .

代码示例:

class MyFormDialog extends React.Component {

state = {

open: false,

};

handleOpen = () => {

this.setState({open: true});

};

handleClose = () => {

this.setState({open: false});

};

render() {

const actions = [

label="Cancel"

onTouchTap={this.handleClose}

/>,

label="Submit"

onTouchTap={this.props.ajaxRequest}

/>,

];

return (

title="Make a request"

actions={actions}

modal={true}

open={this.state.open}

onRequestClose={this.handleClose} />

);

}

}

const mapStateToProps = (state, ownProps) => {

return {

loading: state.isLoading,

success: state.success,

error: state.error,

}

};

const mapDispatchToProps = (dispatch, ownProps) => {

return {

ajaxRequest: (url, tags) => {

dispatch({type: "AJAX_REQUESTED"})

},

}

};

const ConnectedMyFormDialog = connect(

mapStateToProps,

mapDispatchToProps

)(MyFormDialog );

export default ConnectedMyFormDialog ;

Reducers的构建如下:

发送 AJAX_REQUESTED 时

isLoading = true (其他设置为false)

发送 AJAX_SUCCESS 时

success = true (其他设置为false)

发送 AJAX_FAIL 时

error = true (其他设置为false)

所以当 isLoading 从 true 更改为 false 并且 success 更改为 false 更改为 true 时,我想将 state.open 更改为false .

我不知道如何在不搞乱状态的情况下做到这一点 .

编辑:

这是我的传奇代码,用于处理事件:

function* handleAjaxRequest(action) {

try {

yield call(api.doRequest);

yield put({type: "AJAX_SUCCESS"});

} catch (e) {

yield put({type: "AJAX_FAILED"});

}

}

export default function* () {

yield [

takeEvery("AJAX_REQUESTED", handleAjaxRequest),

]

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值