博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git指令大全
阅读量:5122 次
发布时间:2019-06-13

本文共 3419 字,大约阅读时间需要 11 分钟。

一、流程   1、当组件修改数据的时候需要调用store.dispatch传递一个action给store    2、store接受到这个action会将action传递给reducers    3、reducer接受到这个actions后做业务逻辑处理。并返回一个新的newState给store,store这个时候就拥有了state    4、下一次数据再次发生改变的时候就不会使用defaultState 而是用store里面的state    5、需要注意的是 reducers里面必须返回一个纯函数  另外reducers里面的state只允许读不允许修改    6、组件如何监听数据的改变   store.subscribe();监听安装:二、步骤yarn add redux --save-dev1.创建store,对比flux,不需要合并空对象import {createStore, combineReducers} from "redux";import numReducers from "../store/combineReducers/numReducers"import todoReducers from "../store/combineReducers/todoReducers"const reducers = combineReducers({    numReducers,    todoReducers})const store = createStore(reducers);export default store;2.再在store中创建一个reducers.jsreducers必须有一个默认的statereducers必须返回一个纯函数reducers里面的state只允许读,不允许修改,如果修改的话页面是不会发生改变的import reducers from "./reducers"const defaultState = {    inputVal:"",    todoList:[]}export default (state = defaultState,action)=>{    switch(action.type){        case "INPUT_CHANGE":        //深拷贝            const changeState = JSON.parse(JSON.stringify(state))            changeState.inputVal = action.value            return changeState;        case "ADD_ITEM":            const addItemState = JSON.parse(JSON.stringify(state))            addItemState.todoList.push(addItemState.inputVal)            addItemState.inputVal = ""            return addItemState;        case "DEL_ITEM":            const delItemState = JSON.parse(JSON.stringify(state))            delItemState.todoList.splice(action.value,1)            return delItemState    }    return state}3.页面import React,{Component} from "react";import store from "../store"export default class Home extends Component{    constructor(props){        super(props)        this.state = store.getState();        //监听        store.subscribe(this.handleUpdate.bind(this))          }    render(){        let {inputVal,todoList} = this.state        return (            
    { todoList.map((item,index)=>{ return
  • {item}
  • }) }
) } handleChange(e){ let val = e.target.value const action = { type:"INPUT_CHANGE", value:val } store.dispatch(action) } handleAdd(){ const action ={ type:"ADD_ITEM" } store.dispatch(action) } handleDel(params){ const action = { type:"DEL_ITEM", value:params } store.dispatch(action) } handleUpdate(){ this.setState(store.getState()) }}三、通过combineReducers合并多个reducers1.创建combineReducers2.在store>index.js合并import {createStore, combineReducers} from "redux";import numReducers from "../store/combineReducers/numReducers"import todoReducers from "../store/combineReducers/todoReducers"const reducers = combineReducers({ numReducers, todoReducers})const store = createStore(reducers);export default store;3.state后加入reducers名 render(){ //state后加入reducers名 let {inputVal,todoList} = this.state.todoReducers return (

 

转载于:https://www.cnblogs.com/FAB1E/p/10073136.html

你可能感兴趣的文章
JAVA开发环境搭建
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
SDN第四次作业
查看>>
django迁移数据库错误
查看>>
洛谷 1449——后缀表达式(线性数据结构)
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
距离公式汇总以及Python实现
查看>>
Window7上搭建symfony开发环境(PEAR)
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>