Tôi sử dụng phản ứng-redux và redux-saga cho các cuộc gọi API từ ví dụ this. Mục tiêu của tôi là thực hiện một cuộc gọi API khác với các url khác nhau và sử dụng chúng trong các trang khác nhau. Làm thế nào để đạt được điều đó?Nhiều redux-sagas
saga:
import { take, put,call } from 'redux-saga/effects';
import { takeEvery, delay ,takeLatest} from 'redux-saga';
function fetchData() {
return fetch("https://api.github.com/repos/vmg/redcarpet/issues?state=closed")
.then(res => res.json())
.then(data => ({ data }))
.catch(ex => {
console.log('parsing failed', ex);
return ({ ex });
});
}
function* yourSaga(action) {
const { data, ex } = yield call(fetchData);
if (data)
yield put({ type: 'REQUEST_DONE', data });
else
yield put({ type: 'REQUEST_FAILED', ex });
}
export default function* watchAsync() {
yield* takeLatest('BLAH', yourSaga);
}
export default function* rootSaga() {
yield [
watchAsync()
]
}
App:
import React, { Component } from 'react';
import { connect } from 'react-redux';
class App extends Component {
componentWillMount() {
this.props.dispatch({type: 'BLAH'});
}
render(){
return (<div>
{this.props.exception && <span>exception: {this.props.exception}</span>}
Data: {this.props.data.map(e=><div key={e.id}>{e.url}</div>)}
</div>);
}
}
export default connect(state =>({
data:state.data , exception:state.exception
}))(App);
mục tiêu của tôi là để thực hiện một saga, mà tôi sẽ sử dụng trong thành phần khác, và cả hai để không gây rối với nhau . Điều đó có thể?
mỗi xét của bạn về Github [ở đây] (https://github.com/yelouafi/redux-saga/issues/178 # issuecomment-249249953), Tôi đã sửa đổi ví dụ của bạn để hiển thị bằng cách sử dụng hai cuộc gọi API khác nhau. Bạn sẽ có thể mở rộng để làm cho nó hoạt động: http://www.webpackbin.com/VkdjuU02Z –