第二节:react-redux详解分模块调式工具等

Posted 奔跑的路上

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二节:react-redux详解分模块调式工具等相关的知识,希望对你有一定的参考价值。

一. 

 

 

 

 

 

 

 

二. 

 

 

 

 

 

 

 

三. 

 

 

 

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 

react状态管理器(分模块)之redux和redux + react-redux + reducer和redux + react-redux + reducer分模块 + 异步操作redux-thu

1、回顾

cnpm i redux react-redux redux-thunk -S

store/index.js

src/index.js

src/views/home/index.jsx + UI.jsx

2、redux 分模块

2.1 分页面创建页面需要的状态,以首页为例

views/home/reducer.js

const reducer = (state = {
  bannerlist: [],
  prolist: []
}, action) => {
  const { type, data } = action;
  switch (type) {
    case 'CHANGE_BANNER_LIST':
      return { ...state, ...{ bannerlist: data } };
    case 'CHANGE_PRO_LIST':
      return { ...state, ...{ prolist: data } }  
    default:
      return state
  }
}

export default reducer;

views/kind/reducer.js + views/cart/reducer.js

2.2 创建仓库 store/index.js

import { createStore, combineReducers } from 'redux'

import home from '@/views/home/reducer';
import kind from '@/views/kind/reducer';
import cart from '@/views/cart/reducer';

// combineReducers 合并reducer
const reducer = combineReducers({
  home,
  kind,
  cart
})

const store = createStore(reducer);

export default store;

2.3 入口文件处兼容仓库的变化

import store from './store';
function render () {
  ReactDOM.render(
    <Router>
      <Switch>
        <Route path="/" component = { App } />
      </Switch>
    </Router>,
  document.getElementById('root')
  )
}

render()

store.subscribe(render)

2.4 页面组件使用仓库

// 触发状态改变
store.dispatch({type: '', data: ''})
// 获取状态
store.getState() // {home: {}, kind: {}, cart: {}}

3、redux + react-redux + reducer分模块

3.1 页面组件分为容器组件和UI组件 index.jsx + UI.jsx + reducer.js

  • index.jsx
import { connect } from 'react-redux';
import UI from './UI';

export default connect()(UI)
  • UI.jsx 就是各个页面 -- 以首页为例
import React, { Component } from 'react';
export default class extends Component {
  componentDidMount () {
  }
  render () {
    return (
      <div className="box">
        <header className = "header">首页</header>
        <div className = "content">
          首页
        </div>
      </div>
    )
  }
}
  • reducer.js 首页的状态
const reducer = (state = {
  bannerlist: [],
  prolist: []
}, action) => {
  const { type, data } = action;
  switch (type) {
    case 'CHANGE_BANNER_LIST':
      return { ...state, ...{ bannerlist: data } };
    case 'CHANGE_PRO_LIST':
      return { ...state, ...{ prolist: data } }  
    default:
      return state
  }
}

export default reducer;

3.2 store/index.js整合reducer

import { createStore, combineReducers } from 'redux'

import home from '@/views/home/reducer';
import kind from '@/views/kind/reducer';
import cart from '@/views/cart/reducer';

const reducer = combineReducers({
  home,
  kind,
  cart
})

const store = createStore(reducer);

export default store;

3.3 入口文件处处理 仓库

import store from './store';
import { Provider } from 'react-redux';
ReactDOM.render(
  <Provider store={ store }>
    <Router>
      <Switch>
        <Route path="/" component = { App } />
      </Switch>
    </Router>
  </Provider>,
document.getElementById('root')
)

3.4 容器组件获取状态

import { connect } from 'react-redux';
import UI from './UI';

// const mapStateToProps = ({ home: { bannerlist, prolist } }) => ({ bannerlist, prolist })

const mapStateToProps = (state) => {
  return {
    bannerlist: state.home.bannerlist,
    prolist: state.home.prolist
  }
}
export default connect(mapStateToProps)(UI)

3.5 容器组件提供UI组件需要的业务逻辑

import { getBannerlist, getProlist } from '@/utils/api';
const mapDispatchToProps = (dispatch) => {
  return {
    getBannerlist () {
      getBannerlist().then(data => { dispatch({ type: 'CHANGE_BANNER_LIST', data: data.data } ) })
    },
    getProlist () {
      getProlist().then(data => { dispatch({ type: 'CHANGE_PRO_LIST', data: data.data } ) })
    }
  }
}
export default connect(mapStateToProps, mapDispatchToProps)(UI)

3.6 UI组件使用状态和触发业务逻辑

import React, { Component } from 'react';
export default class extends Component {
  componentDidMount () {
    this.props.getBannerlist();
    this.props.getProlist();
  }
  render () {
    let { bannerlist, prolist } = this.props
    return (
      <div className="box">
        <header className = "header">首页</header>
        <div className = "content">
          首页
          { 
            bannerlist.map((item) => (
              <p key={ item.bannerid }> { item.img } </p>
            ))
          }
           { 
            prolist.map((item) => (
              <p key={ item.proid }> { item.proname } </p>
            ))
          }
        </div>
      </div>
    )
  }
}

4、redux + react-redux + reducer分模块 + 异步操作redux-thunk

目前异步操作在容器组件内部,需要把异步操作抽离出来 ---- actionCreator.js

以首页为例
i

4.1 仓库中引入异步操作的中间件 store/index.js

import { createStore, combineReducers, applyMiddleware } from 'redux'
import thunk from 'redux-thunk';//+++++

import home from '@/views/home/reducer';
import kind from '@/views/kind/reducer';
import cart from '@/vews/cart/reducer';

const reducer = combineReducers({
  home,
  kind,
  cart
})

const store = createStore(reducer, applyMiddleware(thunk));//+++++

export default store;

4.2 views/home/actionCreator.js

import { getBannerlist, getProlist } from '@/utils/api';

export default {
  getBannerlist (dispatch) {
    getBannerlist().then(data => {
      dispatch({
        type: 'CHANGE_BANNER_LIST',
        data: data.data
      })
    })
  },
  getProlist (dispatch) {
    getProlist().then(data => {
      dispatch({
        type: 'CHANGE_PRO_LIST',
        data: data.data
      })
    })
  }
}

4.3 容器组件处触发actionCreator

以上是关于第二节:react-redux详解分模块调式工具等的主要内容,如果未能解决你的问题,请参考以下文章

react状态管理器(分模块)之redux和redux + react-redux + reducer和redux + react-redux + reducer分模块 + 异步操作redux-thu

第二十二节 with标签使用详解

关系数据理论-第二节:规范化

Java学习-第一部分-第一阶段-第二节:变量

Java学习-第一部分-第二阶段-第二节:枚举和注释

C#关键字详解第二节