美文网首页
redux 实践总结

redux 实践总结

作者: Mikasa___ | 来源:发表于2020-05-25 09:35 被阅读0次

View 层:React
状态管理:Redux,管理数据状态和UI状态的JavaScript应用工具

Redux工作流.png

举例说明:


image.png

代码实践:
主文件

import React, { Component } from 'react';
import 'antd/dist/antd.css'
import {Input,Button,List} from 'antd'
import store from './store'
import {changeInputAction,addItem,deleteItem} from './store/actionCreators'

//最开始写死了列表组件的内容,后来通过redux createStore(reducer) 里面设置了data这里就不用了
//const data=['one','two','three']

class TodoList extends Component {
    constructor(props){
        super(props)
        this.storeChange = this.storeChange.bind(this)
        this.state=store.getState()
        this.changeInputValue = this.changeInputValue.bind(this)
        this.addList=this.addList.bind(this)
        this.deleteItem=this.deleteItem.bind(this)
        // 订阅状态变化,只要发生变化调用storeChange,获取最新state赋值给state
        store.subscribe(this.storeChange)
        
    }
    
    render() { 
        return ( 
            <div style={{margin:'10px'}}>
            <div>
                <Input placeholder={this.state.inputValue} 
                style={{width:'250px',marginRight:'10PX'}}
                onChange={this.changeInputValue}
                value={this.state.inputValue}
                
                />
                <Button type='primary' onClick={this.addList}>add</Button>
            </div>
            <div style={{margin:'10px',width:'300px'}}>
                <List bordered 
                    
                    
                    // redux createStore(reducer) 里面设置了data这里就不用了
                    //dataSource={data}
                    dataSource={this.state.list}
                    // renderItem 
        renderItem={(item,index)=>(<List.Item onClick={this.deleteItem.bind(this,index)}>{item}</List.Item>)}
            
                /> 
            </div>
            </div>
            
         );
    }
    //input标签内的onChange属性绑定changeInputValue;输入框内容变化则传参e给changeInputValue
    changeInputValue(e){
        // const action = {
        //     // action type区分一个动作的类型
        //     type:CHANGE_INPUT,
        //     value:e.target.value
        // }
        const action=changeInputAction(e.target.value)
        //changeInputValue调用store派遣action方法,action创建了值,把值传给store store接收 自动传给reducers去调度
        store.dispatch(action)
    }

    addList(){
        // const action = {type:ADD_ITEM}
        const action = addItem()
        store.dispatch(action)
    }

    deleteItem(index){
        // const action={type:DELETE_ITEM,index}
        const action=deleteItem(index)
        store.dispatch(action)
    }
    storeChange(){
        this.setState(store.getState)
    }
}
 
export default TodoList;

store/index.js

import {createStore} from 'redux';
import reducer from './reducer';

const store = createStore(reducer,
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    )
export default store

store/reducer.js

import {CHANGE_INPUT,ADD_ITEM,DELETE_ITEM} from './actionTypes'


const defaultState = {
    inputValue:'please input',
    list:['one','two','three']
}

export default (state = defaultState,action)=>{

    console.log(state,action)
    //Reducer里只接收state不能直接改变state,可以用中间变量来改变
    if(action.type===CHANGE_INPUT){
        //JSON.parse(JSON.stringify()) ES6深度拷贝
        let newState = JSON.parse(JSON.stringify(state))
        newState.inputValue=action.value
        return newState

    }
    if(action.type===ADD_ITEM){
        //JSON.parse(JSON.stringify()) ES6深度拷贝
        let newState = JSON.parse(JSON.stringify(state))
        newState.list.push(newState.inputValue)
        newState.inputValue=''
        return newState

    }

    if(action.type===DELETE_ITEM){
        //JSON.parse(JSON.stringify()) ES6深度拷贝
        let newState = JSON.parse(JSON.stringify(state))
        newState.list.splice(action.index,1)
        
        return newState

    }
    
    return state
}

相关文章

  • redux 实践总结

    View 层:React状态管理:Redux,管理数据状态和UI状态的JavaScript应用工具 举例说明: 代...

  • dva 框架介绍

    dva 是基于 redux 最佳实践 实现的 framework,简化使用 redux 和 redux-saga ...

  • React和Redux的连接react-redux

    通过Redux文档来了解react-redux,在一段时间的实践后准备翻一翻源代码,顺便做些相关的总结。我看的代码...

  • react-redux实践

    react-redux实践 了解 1、什么是redux 官方解释:redux 是 js 应用的可预测状态的容器。 ...

  • Retalk,Redux 从未如此简单

    简介 Retalk 是 Redux 的一个最佳实践,简单、流畅而智慧。 特性 极简 Redux 实践:只需要 st...

  • React+Redux工程目录结构,最佳实践

    参考 Redux进阶系列1: React+Redux项目结构最佳实践 《深入浅出React和Redux》一书的第四...

  • Redux 实践

    为了更深刻理解redux,我们将使用一个简化的购物车项目来学习Redux。 组织Redux代码 首先,在src文件...

  • React入门(七)Redux(二)

    本文内容 概述Redux原理 总结Redux使用 (一)概述Redux原理 个人理解Redux 里面由两部分组成s...

  • Flutter redux 使用与理解

    Flutter 状态管理redux 方案理解与实践 redux 数据管理方式来自 react ,React 的数据...

  • Redux Persist 最佳实践

    Redux Persist 最佳实践 希望可以帮助大家使用和了解redux-persist的相关能力 接入 npm...

网友评论

      本文标题:redux 实践总结

      本文链接:https://www.haomeiwen.com/subject/ujjdahtx.html