关于react.js:react-状态管理

redux

一、什么时候用到redux
1、用户的应用形式简单1
2、不同身份的用户有不同的应用形式(比方普通用户和管理员)
3.多个用户之间合作
4、与服务器大量交互
5、 view要从多个起源获取数据

二、 繁多数据源
1、整个利用的state被存储在一个object tree中,并且这个object tree 只存在于惟一store中
2、state只读
惟一扭转state的办法就是action,action是一个用于形容已产生事件的一般对象
3、应用纯函数来执行批改
为了形容action如何扭转state tree ,你须要编写reducers

store有以下职责
维持利用的state
提供getState()办法获取state
提供dispatch(action)办法更新state
通过subscribe(listener)注册监听器
通过subscribe(listener)返回的函数登记监听器

import { createStore } from 'redux'
const initState = {
  count: 0
}

function reducer(state = initState,action){
  switch(action.type){
    case 'cadd':
      return {
        ...state,
        count: state.count + action.count
      }
    case 'decre':
      return {
        ...state,
        count: state.count - action.count
      }
    default:
      return {
        ...state
      }
  }
}

const store = createStore(reducer);

expoort default store;


import React,{Component} from 'react';
import Store  from 'store';

class Count extends Component {
  add(){
    store.dispatch({type:'incre', count:5})
  }
  componentDidMount(){
    this.clear = store.subscribe(() => {  //store.subscribe 触发监听state的更新 // 返回一个函数 革除监听
      this.setState({})
    })
  }
  componentDidUpdate(){
    this.clear() // 革除 
  }
  render(){
    let state = store.getState;
    return (
      <div>
        <h1>{state.count}</h1>
        <button onClick={this.add.bind(this)}>incre</button>
      </div>
    )
  }
}

简略本人实现一个reducer

export function createStore(reducer){
  let state;
  let eventListeners = [] // 事件池
  function getState(){
    return state;
  }
  function dispatch(action){
    state = reducer(state,action);
    eventListeners.forEach(item => {
      item && item();
    })
  }
  function subscribe(fn){
    eventListeners.psush(fn)
    return (fn) => {
      eventListeners.filter((item) => item === fn)
    }
  }
  dispatch({}) // 初始化state的数据,通过执行reducer
  return {
    getState,
    dispatch,
    subscribe,
  } 
}

本人实现react-reducer

// 本人实现react-redux
 import React, { Component } from 'react'
 export const MyContext = React.createContext();

 export class Provider extends Component{
   render(){
     let {children, sotre } = this.props;
     return <MyContext value={store}>
      {children}
     </MyContext>
   }
 }

 export function connect(mapStateToProps, mapDispatchToProps){
  return function(comp){
    return class Temp extends Component{
      constructor(props, context){ // 此处的context 是MyContext传进来的
        super(props,context);
        this.state = mapStateToProps(context.getState())
        this.dispatch = mapDispatchToProps(context.dispatch);
      }
      static contextType = MyContext;  // 动态属性  此时本地就有this.context
      componentDidMount(){
        this.context.subscribe(() => {
          this.setState( // 更新,从新渲染
            mapStateToProps(this.context.getState())
          )
        })
      }
      render(){
        return <Comp {...this.props} {...this.state} {...this.dispatch} />
      }
    }
  }
 }

combineReducers

 function reducer1(){
   //...
 }

 function reducer2 (){
   //...
 }

 export default store = createStore(combineReducers({
   reducer1,
   reducer2
 })) 
// 实现原理
 export function combineReducers(obj) {  // 返回值是一个reducer办法
    return function(state = {},action){
      Object.keys(obj).forEach((key) => {
        state[key] = obj[key](state[key], action)
      })
      return state
    }
 }

mobox

灵便
性能 哪更新哪加载

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理