一些动作具有异步功能,例如提取。但是,我不想使用像redux-thunk或redux-saga这样的中间件。因此,我不愿意使用此代码。
/* actions */
...
export const fetchRequest = ({category, query, dispatch}) => ({
type: actionTypes.FETCH_REQUEST,
promise:
fetch(`${API_URL}${category}?${query}`, {headers: headers})
.then(response => response.json())
.then(data => dispatch(fetchRecieve(data))),
})
export const fetchRecieve = data => ({
type: actionTypes.FETCH_RECIEVE,
data,
})
...
/* In x.jsx */
...
const mapDispatchToProps = dispatch => ({
onClick: (category, query) => dispatch(fetchRequest({category, query, dispatch}))
})
...
Run Code Online (Sandbox Code Playgroud)
Redux范式是否违反了此代码?