页面三种状态 success , request, failure

    1. import React from 'react';
    2. import {connect} from 'react-redux'
    3. import {bindActionCreators} from 'redux'
    4. import * as userActions from '../actions/user'
    5. class User extends React.Component{
    6. render() {
    7. let {error, isFetching, user} = this.props.user
    8. let data
    9. if (error) {
    10. data = error
    11. } else if (isFetching) {
    12. data='loading....'
    13. } else {
    14. console.log(this.props.user.userInfo)
    15. data = user.title
    16. }
    17. return (
    18. <div className='container' >
    19. <p className='text-center'>我是 user {this.props.name}</p>
    20. <p className='text-center'>userInfo: {data}</p>
    21. <button onClick={() => this.props.userActions.getUser()}>获取数据</button>
    22. </div>
    23. )
    24. }
    25. }
    26. const mapStateToProps = (state) => {
    27. // console.log(state)
    28. return {
    29. user: state.user
    30. }
    31. }
    32. const mapDispatchToProps = (dispatch) => {
    33. return {
    34. userActions: bindActionCreators(userActions, dispatch)
    35. }
    36. }
    37. export default connect(mapStateToProps, mapDispatchToProps)(User)
    • actions/index.js
    1. import {FETCH_USER, FETCH_FAILURE, FETCH_SUCCESS,FETCH_REQUEST} from '../constants'
    2. export function fetchUser(user) {
    3. return {
    4. type: FETCH_SUCCESS,
    5. user
    6. }
    7. }
    8. export function fetchRequest() {
    9. return {
    10. type: FETCH_REQUEST
    11. }
    12. }
    13. export function fetchFailure(error) {
    14. return {
    15. type: FETCH_FAILURE,
    16. error
    17. }
    18. }
    19. // 触发函数执行一定要用dispatch
    20. export const getUser = () => {
    21. return dispatch => {
    22. dispatch(fetchRequest())
    23. fetch('https://douban.uieee.com/v2/movie/in_theaters').then(res => res.json()).then(data => {
    24. dispatch(fetchUser(data))
    25. }).catch(err => {
    26. // console.log(err)
    27. dispatch(fetchFailure(err))
    28. })
    29. }
    30. }
    • reducer/user.js
    1. import {FETCH_USER, FETCH_SUCCESS, FETCH_FAILURE,FETCH_REQUEST} from '../constants'
    2. // reducer是个纯函数, 改变store数据的
    3. // 数据在action 中
    4. // 改变对象
    5. const initState = {
    6. user: {
    7. title: 1
    8. },
    9. isFetching: false,
    10. error: null
    11. }
    12. // state 只读的,必须返回一个新对象
    13. const counter = (state = initState, action) => {
    14. switch(action.type) {
    15. case FETCH_SUCCESS:
    16. return {
    17. isFetching: false,
    18. error: null,
    19. user: action.user
    20. }
    21. case FETCH_REQUEST:
    22. return {
    23. isFetching: true,
    24. error: null,
    25. user: {}
    26. }
    27. case FETCH_FAILURE:
    28. return {
    29. isFetching: false,
    30. error: action.error,
    31. user: {}
    32. }
    33. default:
    34. return state
    35. }
    36. }
    37. export default counter
    • 组件中使用
    1. import React from 'react';
    2. import {connect} from 'react-redux'
    3. import {bindActionCreators} from 'redux'
    4. import * as userActions from '../actions/user'
    5. class User extends React.Component{
    6. render() {
    7. let {error, isFetching, user} = this.props.user
    8. let data
    9. if (error) {
    10. data = error
    11. } else if (isFetching) {
    12. data='loading....'
    13. } else {
    14. console.log(this.props.user.userInfo)
    15. data = user.title
    16. }
    17. return (
    18. <div className='container' >
    19. <p className='text-center'>userInfo: {data}</p>
    20. <button onClick={() => this.props.userActions.getUser()}>获取数据</button>
    21. </div>
    22. )
    23. }
    24. }
    25. const mapStateToProps = (state) => {
    26. // console.log(state)
    27. return {
    28. user: state.user
    29. }
    30. }
    31. const mapDispatchToProps = (dispatch) => {
    32. return {
    33. userActions: bindActionCreators(userActions, dispatch)
    34. }
    35. }
    36. export default connect(mapStateToProps, mapDispatchToProps)(User)