redux action ajax,react+redux,异步请求为什么要写在action creator里面?

在redux官方文档里面,针对异步给出的方案是:

export function fetchPosts(subreddit) {

return function (dispatch) {

dispatch(requestPosts(subreddit));//准备发起请求

return fetch(`http://www.subreddit.com/r/${subreddit}.json`)

.then(response => response.json())

.then(json => dispatch(receivePosts(subreddit, json)))//拿到请求结果

}

}

主要思路是:

这是一个特殊的action creator(返回了一个function)

在这个action creator里面,可以dispatch其他action

通过redux-thunk中间件,可以dispatch(fetchPosts('sth'))

为什么异步请求不能直接写在容器组件的mapDispatchToProps里面?

const mapDispatchToProps = dispatch => ({

fetchTest: () => {

dispatch(fetch_request());//准备发起请求

fetch('/package.json')

.then(response => response.json())

.then(json => dispatch(fetch_success(json)))//拿到请求结果

}

});

@connect(mapStateToProps, mapDispatchToProps)

export default class MyContainer extends Component {

render() {

return (

)

}

}

//Fetchbtn

handleClick = (e) => {

this.props.fetchTest();

}

感觉

1.不用引中间件(redux-thunk、redux-promise)了,也不用考虑中间件执行顺序等问题。

2.逻辑写在容器组件里面,感觉没什么毛病,UI组件如果需要,都可以拿到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Redux中处理异步数据通常需要使用中间件来处理异步操作。最常用的中间件是Redux Thunk和Redux Saga。 使用Redux Thunk时,你可以在action creator中返回一个函数,而不仅仅是一个普通的action对象。这个函数可以接收dispatch和getState作为参数,并且可以在内部进行异步操作。例如,你可以使用axios库来发起异步请求,并在请求完成后分发相应的action。 下面是一个使用Redux Thunk处理异步数据的示例: ```javascript // 安装并配置Redux Thunk中间件 import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from './reducers'; const store = createStore(rootReducer, applyMiddleware(thunk)); // 定义一个异步的action creator const fetchPosts = () => { return (dispatch, getState) => { dispatch({ type: 'FETCH_POSTS_REQUEST' }); axios.get('/api/posts') .then(response => { dispatch({ type: 'FETCH_POSTS_SUCCESS', payload: response.data }); }) .catch(error => { dispatch({ type: 'FETCH_POSTS_FAILURE', payload: error.message }); }); }; }; // 在组件中调用异步action creator import { connect } from 'react-redux'; import { fetchPosts } from './actions'; class PostList extends React.Component { componentDidMount() { this.props.fetchPosts(); } render() { // 渲染帖子列表 } } export default connect(null, { fetchPosts })(PostList); ``` Redux Saga是另一个处理异步操作的中间件,它基于Generator函数和ES6的yield关键字来实现。它提供了更强大和可扩展的异步控制流。使用Redux Saga时,你可以定义一系列的saga函数来处理各种异步操作,例如API请求、定时器等。 以下是一个使用Redux Saga处理异步数据的示例: ```javascript // 安装并配置Redux Saga中间件 import { createStore, applyMiddleware } from 'redux'; import createSagaMiddleware from 'redux-saga'; import rootReducer from './reducers'; import rootSaga from './sagas'; const sagaMiddleware = createSagaMiddleware(); const store = createStore(rootReducer, applyMiddleware(sagaMiddleware)); sagaMiddleware.run(rootSaga); // 定义一个saga函数来处理异步操作 import { call, put, takeEvery } from 'redux-saga/effects'; import axios from 'axios'; function* fetchPosts() { try { yield put({ type: 'FETCH_POSTS_REQUEST' }); const response = yield call(axios.get, '/api/posts'); yield put({ type: 'FETCH_POSTS_SUCCESS', payload: response.data }); } catch (error) { yield put({ type: 'FETCH_POSTS_FAILURE', payload: error.message }); } } // 定义根Saga函数 export default function* rootSaga() { yield takeEvery('FETCH_POSTS', fetchPosts); } // 在组件中分发异步action import { connect } from 'react-redux'; import { fetchPosts } from './actions'; class PostList extends React.Component { componentDidMount() { this.props.fetchPosts(); } render() { // 渲染帖子列表 } } export default connect(null, { fetchPosts })(PostList); ``` 无论你选择使用Redux Thunk还是Redux Saga,它们都提供了一种在React Redux中处理异步数据的方式。你可以根据自己的需求选择合适的中间件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值