나는 react/redux/react-redux를 사용하여 ajax 요청을하는 모달 폼을 구현하고 있습니다.react-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 = [
<FlatButton
label="Cancel"
onTouchTap={this.handleClose}
/>,
<FlatButton
label="Submit"
onTouchTap={this.props.ajaxRequest}
/>,
];
return (
<div>
<MenuItem primaryText="Make a request" onTouchTap={this.handleOpen}/>
<Dialog
title="Make a request"
actions={actions}
modal={true}
open={this.state.open}
onRequestClose={this.handleClose} />
</div>
);
}
}
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 ;
기어는 다음과 같은 내장되어 : AJAX_REQUESTED
보낼 때
isLoading = true
는success = true
이AJAX_SUCCESS
가 (다른 사람을 보낼 때 (다른 사람이 false로 설정되어 있습니다) false로 설정 됨)error = true
AJAX_FAIL
는
그래서 내가 거짓으로 state.open
을 변경하려면 (다른 사람이 false로 설정되어 있습니다)를 보낼 때 false
에 true
에서 isLoading
변화와 true
에 false
-success
변화.
나는 상태를 망치지 않고 어떻게하는지 잘 모릅니다.
편집 :
여기내 무용담 코드는, 처리하는 이벤트 :
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),
]
}
고마워, 그것은 잘 작동했다 :-) – Blusky