09.使用react-thunk解决异步action问题

09.使用react-thunk解决异步action问题 - 图1

使用方式

  1. import { createStore, applyMiddleware } from 'redux'
  2. import thunk from 'redux-thunk'
  3. const store = createStore(weight, applyMiddleware(thunk))
  1. export const eatAsync = () =>{
  2. return dispatch =>{
  3. setTimeout(()=>{
  4. // dispatch(eat('煎饼'))
  5. dispatch({type: EAT})
  6. },2000)
  7. }
  8. }

源码

  1. import { createStore } from 'redux'
  2. const EAT = 'eat'
  3. const HUNGRY = 'hungry'
  4. // reducer
  5. export const weight = (state = 160 , action) => {
  6. switch (action.type) {
  7. case EAT:
  8. return state + 10
  9. case HUNGRY:
  10. return state - 10
  11. default:
  12. return 160
  13. }
  14. }
  15. // const store = createStore(weight)
  16. // action
  17. export const eat = (data) => {
  18. console.log('stark wang 正在吃'+ data)
  19. return {type: EAT}
  20. }
  21. export const hungry = () => {
  22. console.log('饿了一天')
  23. return {type: HUNGRY}
  24. }
  25. export const eatAsync = () =>{
  26. return dispatch =>{
  27. setTimeout(()=>{
  28. dispatch(eat('煎饼'))
  29. },2000)
  30. }
  31. }
  1. import React from 'react'
  2. import logo from './logo.svg'
  3. import './App.css'
  4. import { eat, hungry, eatAsync } from './stark.redux'
  5. import { connect } from 'react-redux'
  6. class App extends React.Component {
  7. render () {
  8. const {num, eat, hungry , eatAsync} = this.props
  9. return (
  10. <div className='App'>
  11. <header className='App-header'>
  12. <img src={logo} className='App-logo' alt='logo' />
  13. <p>
  14. redux完全指南】系列1:从入门到会用
  15. <br></br>
  16. hi gusy I am stark
  17. </p>
  18. <a
  19. className='App-link'
  20. href='https://shudong.wang'
  21. target='_blank'
  22. rel='noopener noreferrer'>跟着stark wang redux</a>
  23. <h2>stark wang 当前的体重为{num}斤</h2>
  24. <button onClick={() => {eat()}}>
  25. 吃了一坨山珍海味
  26. </button>
  27. <button onClick={() => {eatAsync()}}>
  28. 等了一会,吃了一个煎饼
  29. </button>
  30. <button onClick={() => {hungry()}}>
  31. 饿了一天
  32. </button>
  33. </header>
  34. </div>
  35. )
  36. }
  37. }
  38. // const mapStateToProps = (state)=>{
  39. // return {num:state}
  40. // }
  41. // const actionCreators = {eat,hungry}
  42. export default connect((state)=>{
  43. return {num:state}
  44. },{eat,hungry,eatAsync})(App)
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import App from './App';
  5. import * as serviceWorker from './serviceWorker';
  6. import { createStore, applyMiddleware } from 'redux'
  7. import { weight } from './stark.redux'
  8. import { Provider } from 'react-redux'
  9. import thunk from 'redux-thunk'
  10. const store = createStore(weight, applyMiddleware(thunk))
  11. // const render = () =>{
  12. ReactDOM.render(
  13. <Provider store={store}>
  14. <App />
  15. </Provider>,
  16. document.getElementById('root')
  17. );
  18. // }
  19. // render()
  20. // store.subscribe(render)
  21. // If you want your app to work offline and load faster, you can change
  22. // unregister() to register() below. Note this comes with some pitfalls.
  23. // Learn more about service workers: https://bit.ly/CRA-PWA
  24. serviceWorker.unregister();