参考文档

redux.png

一、安装依赖

  1. yarn add redux

二、项目配置

  1. store
  2. --index.js
  3. --reducer.js

2-1 创建store(容器),管理数据

  1. //index.js
  2. import {createStore} from 'redux';
  3. import reducer from './reducer';
  4. const store = createStore(reducer);
  5. export default store;

2-2 创建reducer,接收action,重新计算state

  1. let defaultState={
  2. msg:"redux",
  3. list:[]
  4. }
  5. // reducer可以接受state,但是不能直接修改state
  6. export default (state=defaultState,action)=>{
  7. switch (action.type) {
  8. case "change_input_value":
  9. let newState = {...state};
  10. newState.msg = action.value
  11. return newState;
  12. case "add_todo_item":
  13. let nState = {...state};
  14. if(nState.msg!==""){
  15. nState.list.push(nState.msg)
  16. nState.msg = ""
  17. }
  18. return nState
  19. case "delete_todo_item":
  20. let yState = {...state};
  21. yState.list.splice(action.value,1)
  22. return yState
  23. default:
  24. return state
  25. }
  26. }

2-3 在Home.js导入store(需要在那个组件中使用,就在那里导入)

  1. import store from '../store'js
  2. class Home extends Component {
  3. constructor(props) {
  4. super(props);
  5. this.state = getState();
  6. }
  7. ....
  8. }

2-4 Action

View发出的一个通知,要改变state

  1. //通过一个事件派发action
  2. 1.创建一个ation,同时派发
  3. import React, { Component } from 'react';
  4. import store from '../store'
  5. class Home extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = store.getState();
  9. }
  10. render() {
  11. return (
  12. <div>
  13. 首页
  14. <button onClick={this.handleClick}>改变store</button>
  15. </div>
  16. );
  17. }
  18. handleClick=()=>{
  19. const action = {
  20. type:"btn_change",
  21. value:"复习redux"
  22. }
  23. store.dispatch(action)
  24. }
  25. }
  26. export default Home;
  1. //2.store自动接收action,之后将action传递给reducer
  1. //3.在reducer改变state
  2. let defaultState = {
  3. msg:"react"
  4. }
  5. //reducer可以接收state,但是不能直接修改state
  6. export default (state=defaultState,action)=>{
  7. console.log(action)
  8. switch(action.type){
  9. case "btn_change":
  10. var newState = {...state};
  11. newState.msg = action.value;
  12. return newState;
  13. default:
  14. return state;
  15. }
  16. }
  1. //4.订阅store的状态的改变,只要store改变,store.subscribe(fn),fn函数必然会执行
  2. class Home extends Component {
  3. constructor(props) {
  4. ...
  5. store.subscribe(this.handleStoreChange);
  6. }
  7. ...
  8. handleStoreChange=()=>{
  9. console.log(1)
  10. this.setState(store.getState())
  11. }
  12. }

三、基本概念

  1. store 就是保存数据的地方,将store看成一个容器,一个应用只能有一个store
  2. //如何生成一个store createStore
  1. //store--index.js
  2. import { createStore } from 'redux'
  3. const defaultState = {
  4. msg:"react很不好用"
  5. }
  6. const reducer = (state=defaultState,action)=>{
  7. return state;
  8. }
  9. // createStore只能接收函数
  10. let store = createStore(reducer);
  11. export default store;
  1. reducer 就是对state重新计算的过程就是reducer,reducer必须是函数

四、redux的调试

  1. 1.chrome应用商店安装redux
  1. https://github.com/zalmoxisus/redux-devtools-extension
  2. //2.配置
  3. import { createStore } from 'redux'
  4. import reducer from './reducer'
  5. // createStore只能接收函数
  6. let store = createStore(reducer,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
  7. export default store;