1.redux搭建

安装redux

  1. yarn add redux

2.store

  1. //该文件用于暴露store对象
  2. //引入createStore,专门用于创建redux中最核心的store对象
  3. import {createStore} from 'redux'
  4. //引入为count组件服务的reducer
  5. import countReducer from './count_reducers'
  6. //store中需要一个为之服务的reducer,因此将引入的reducer当参数传入这个创建的store中
  7. //并且将这个store对象暴露出去
  8. export default createStore(countReducer)

3.reducer

  1. //创建一个为组件服务的reducer,它的本质是一个函数
  2. //定义个初始值
  3. let initState=0
  4. //传入两个参数,一个为之前的状态preState,另一个是动作对象action
  5. //设置默认值,初始化数据data
  6. //这里的reducer是一个纯函数
  7. export default function countReducers(preState =initState, action) {
  8. //从action中拿到type与data
  9. const {
  10. type,
  11. data
  12. } = action
  13. //根据type加工数据
  14. switch (type) {
  15. case 'increment':
  16. return preState + data
  17. case 'decrement':
  18. return preState - data
  19. default:
  20. return preState
  21. }
  22. }

4.action

  1. //分别封装两个action函数用来创建action对象
  2. export const createIncrement=data=>({type:"increment",data})
  3. export const createDecrement=data=>({type:"decrement",data})

5.constant

  1. //定义常量文件
  2. export const INCREMENT='increment'
  3. export const DECREMENT='decrement'

6.使用

6.1 引入store

  1. import store from '../../redux/count_redux/store'

6.2通过getstate可以获取初始状态

  1. <h1>当前显示的数是:{store.getState()}</h1>

6.4引入action

  1. import {createIncrement,createDecrement} from '../../redux/count_redux/count_action'

6.3使用dispatch去传递action

  1. //通知redux去加value
  2. store.dispatch(createIncrement(value*1))

6.4渲染render

  1. 因为没有setdata,页面没有发生更新,需要监听redux,动态的去触发render函数
  1. 使用setstate中去触发render
  2. componentDidMount(){
  3. store.subscribe((()=>{
  4. this.setState({})
  5. }))
  6. }
  1. 重新渲染根组件,触发render
  2. store.subscribe(()=>{
  3. ReactDOM.render(
  4. <App />,
  5. document.getElementById('root')
  6. );
  7. })

7.案例

  1. import React, { Component } from 'react'
  2. // 引入store用于获取store状态
  3. import store from '../../redux/count_redux/store'
  4. import {createIncrement,createDecrement} from '../../redux/count_redux/count_action'
  5. export default class index extends Component {
  6. componentDidMount(){
  7. store.subscribe((()=>{
  8. this.setState({})
  9. }))
  10. }
  11. //加
  12. increment=()=>{
  13. let {value}=this.selectNumber
  14. //通知redux去加value
  15. store.dispatch(createIncrement(value*1))
  16. }
  17. //减
  18. decrement=()=>{
  19. let {value}=this.selectNumber
  20. //通知redux去加value
  21. store.dispatch(createDecrement(value*1))
  22. }
  23. //异步加
  24. asyncIcrement=()=>{
  25. let {value}=this.selectNumber
  26. setTimeout(() => {
  27. store.dispatch(createIncrement(value*1))
  28. }, 500);
  29. }
  30. //奇数加
  31. oddIcrement=()=>{
  32. let {value}=this.selectNumber
  33. let count=store.getState()
  34. if(count%2!==0){
  35. store.dispatch(createIncrement(value*1))
  36. }
  37. }
  38. render() {
  39. return (
  40. <div>
  41. <h1>当前显示的数是:{store.getState()}</h1>
  42. <select ref={c=>this.selectNumber=c}>
  43. <option value="1">1</option>
  44. <option value="2">2</option>
  45. <option value="3">3</option>
  46. </select>
  47. <button onClick={this.increment}>加</button>&nbsp;&nbsp;&nbsp;
  48. <button onClick={this.decrement}>减</button>&nbsp;&nbsp;&nbsp;
  49. <button onClick={this.asyncIcrement}>异步加</button>&nbsp;&nbsp;&nbsp;
  50. <button onClick={this.oddIcrement}>奇数加</button>&nbsp;&nbsp;&nbsp;
  51. </div>
  52. )
  53. }
  54. }