redux在react-native上使用(三)--加入redux-thunk
发布时间:2020-12-15 07:29:24 所属栏目:百科 来源:网络整理
导读:这篇 redux在react-native上使用(二)--加入saga 是使用 redux-saga ,可以跟这篇做个对比看下 redux-thunk 和 redux-saga 使用上的区别. 直接在这项目上修改,只是把 redux-thunk 替换了 redux-saga ,还是达到一样的项目. 首先在 package.json 里添加 redux-th
这篇 redux在react-native上使用(二)--加入saga 是使用 直接在这项目上修改,只是把 首先在 "dependencies": { ... "redux-thunk": "^2.2.0" }, 把 import { createStore,applyMiddleware,compose } from 'redux'; import createLogger from 'redux-logger'; import thunk from 'redux-thunk'; import rootReducer from './reducers'; const configureStore = preloadedState => { return createStore ( rootReducer,preloadedState,compose ( applyMiddleware(thunk,createLogger()) ) ); } const store = configureStore(); export default store;
import { START,STOP,RESET,RUN_TIMER } from './actionsTypes'; const startAction = () => ({ type: START }); const stopAction = () => ({ type: STOP }); const resetAction = () => ({ type: RESET }); const runTimeAction = () => ({ type: RUN_TIMER }); var t = -1; export const start = ()=> { return dispatch => { dispatch(startAction()); if(t != -1) return; t = setInterval(() => { dispatch(runTimeAction()); },1000); }; } export const stop = ()=> { return dispatch => { dispatch(stopAction()); if (t != -1) { clearInterval(t); t = -1; } } } export const reset = ()=> { return dispatch => { dispatch(resetAction()); dispatch(stop()); } } OK,大功告成, (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |