【发布时间】:2018-07-13 09:59:13
【问题描述】:
我正在将我的 react 项目转移到 redux 和 redux-saga。最初,我调用异步方法来获取大型数据集,然后将其设置为本地状态,如下所示:
// Component.jsx
componentDidMount() {
const dataPromise = this.getTableData()
const data = await dataPromise
this.setState({ data })
}
getTableData = async() => {
const response = await APIUtils.getTableData()
let data = null
if (response && response.code === "200") {
data = response.data
}
return data
}
现在使用 redux,我正在这样更改它
// Component.jsx
componentDidMount() {
const data = this.props.getTableData() // how to get data here?
this.setState({ data })
}
// ActionCreator.js
function getTableData() {
return {
type: "GET_TABLE_DATA"
}
}
// saga.js
function *getTableData() {
try {
const response = yield call(APIUtils.getTableData)
...
// here I want to send this response.data to my comp without calling store action as the dataset is large and it is read-only.
} catch (err) {
yield put(showError(false))
}
}
export default function* root() {
yield all([
takeLatest("GET_TABLE_DATA", getTableData)
])
}
我是 redux-saga 的新手,谁能告诉我最好的方法是什么。
【问题讨论】:
标签: reactjs redux yield redux-saga