redux模块化demo
store.js 在redux中 store 是唯一的。
import {createStore} from 'redux';
import reducer from './reducer'
// 引入后的reducer store是唯一的
const store = createStore(reducer);
export default store;
reduce.js 合并所以reducer
import {combineReducers} from 'redux';
import numReducer from './num/reducer'
import todosReducer from './todos/reducer';
// 拿到单个模块的reducer 进行合并 传给store
let reducer = combineReducers({
num:numReducer,
todos:todosReducer
});
export default reducer;
在todos模块(文件夹)下
state.js
// 用来存储当前模块下的数据
const state = {
list:[],
count:0
} export default state;
reducer.js
// 将state导入 建立reducer函数
import _state from './state'; let reduce = (state=_state,action)=>{
console.log(action);
let newState = {...state};
if(action.type==='ADD'){
newState.count = ++newState.count;
newState.list.push(action.title)
}
return newState;
}
// reducer 用来处理state里面的数据 数据的验证是通过action这个参数里的type进行的。
// action这个参数的传递是通过store.dispatch来分发的。
export default reduce;
action.js
// 主要作用是返回一个对象 让actions 来使用
// p 是传递的参数
const action = {
ADD(p){
return {
type :'ADD', //这里的ADD的type是与reducer里的验证有关
title:p
}
}
}
export default action;
actions.js
import action from './action';
import store from '../store';
// 将传递的action参数引入
// 将store引入 把action参数传给reducer。 const actions = {
// p 是页面传来的值
addItem(p){
// 将action的里面对象传递参数
let act = action.ADD(p);
// 使用store把action里面的对象 作为参数传递过去
store.dispatch(act);
}
} export default actions;
App.js
import React, { Component } from 'react';
import './App.css';
import store from './redux/store'
import actions from './redux/num/actions'
import actions1 from './redux/todos/actions'
// ui 组件 只取数据
class App extends Component {
constructor(props){
super(props);
this.state = {
n:store.getState().num.n,
list:store.getState().todos.list
}
store.subscribe(()=>{
//只要数据变化了这个回调函数就会执行
this.setState({
n:store.getState().num.n
});
this.setState({
list:store.getState().todos.list
})
})
this.inc = this.inc.bind(this);
this.add = this.add.bind(this);
}
inc(){
// console.log(actions.dispatchAction)
actions.dispatchAction()
}
add(){
actions1.addItem(this.node.value);
this.node.value = '';
}
render() {
return (
<div className="App" onClick={this.inc}>
{ this.state.n}
<input type="text" ref={node=>this.node=node}/>
<button onClick={this.add}>add</button>
{
this.state.list.map((item,index)=>{
return (
<div key={index}>
{item}
</div>
)
})
}
</div>
);
}
}
export default App;
redux模块化demo的更多相关文章
- redux源码解析(深度解析redux+异步demo)
redux源码解析 1.首先让我们看看都有哪些内容 2.让我们看看redux的流程图 Store:一个库,保存数据的地方,整个项目只有一个 创建store Redux提供 creatStore 函数来 ...
- 我的第一个 react redux demo
最近学习react redux,先前看过了几本书和一些博客之类的,感觉还不错,比如<深入浅出react和redux>,<React全栈++Redux+Flux+webpack+Bab ...
- vuex与redux,我们都一样
vuex与redux的主要区别: redux:生成的全局数据流是通过每个组件的props逐层传递到各个子组件的,通过@connect装饰器绑定在this.props上面. vuex :生成的全局数据则 ...
- Qt+腾讯IM开发笔记(一):腾讯IM介绍、使用和Qt集成腾讯IM-SDK的工程模板Demo
前言 开发一个支持全国的IM聊天,可以有基本的功能,发送文本.图片.文件等等相关内容. 腾讯IM产品 概述 腾讯即时通信IM是腾讯推出的即时聊天程序,当前时间为2020年3月(腾讯IM的优 ...
- FEE Development Essentials
FEE Development Essentials JS Basic function call() and apply() func1.bind(thisObj,arg1...argn) Cust ...
- React 页面间传值的个人总结
react 组件之间传值的方案有很多,下面是我个人经验的总结 props 来传递值 传值方式: 通过props 获取值 通过props 提供的func去修改值 优点: 不需要任何第三方的组件,纯rea ...
- 小程序开发:用Taro搭建框架
1.node环境 1) 下载 . 官方地址:https://nodejs.org/en/ 或 https://nodejs.org/zh-cn/ 2)安装. 一路next......Install.直 ...
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
随机推荐
- 【Linux基础】VI 编辑器基本使用方法
vi编辑器是所有Unix及Linux系统下标准的编辑器.对Unix及Linux系统的任何版本,vi编辑器是完全相同的,它是Linux中最基本的文本编辑器. 第一章vi的三种模式 第二章vi文本编辑器 ...
- Codeforces Round #542 B Two Cakes
B. Two Cakes time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Linux三剑客-SED
1.Sed是什么 Sed:字符流编辑器,Stream Editor 2.Sed功能与版本 处理日志文件,日志,配置文件等 增加.删除.修改.查询 sed --version 可以通过man sed 来 ...
- Oracle hint之ORDERED和USE_NL
Hint:ORDERED和USE_NL ORDERED好理解,就是表示根据 from 后面表的顺序join,从左到右,左边的表做驱动表 use_nl(t1,t2):表示对表t1.t2关联时采用嵌套循环 ...
- hadoop学习笔记肆--元数据管理机制
1.首先,认识几个名词 (1).NameNode中读.写.以及DataNode映射等信息叫做“元数据” ,NameNode元数据存放位置有.内存.fsimage.edits log三个位置. (2). ...
- matlab中fix函数,floor函数,ceil函数
1)fix(n)的意义是取小于n的整数(是向零点舍入的意思是往零的方向上靠),这是一类应用在整数取值上的函数,就如同以前我们所研究的求整问题,例如,fix(pi)=3;fix(3.5)=3;fix(- ...
- face recognition[Euclidean-distance-based loss][FaceNet]
本文来自<FaceNet: A Unified Embedding for Face Recognition and Clustering>.时间线为2015年6月.是谷歌的作品. 0 引 ...
- UIImageView - BNR
继续上节UINavigationController - BNR. 打开BNRDetailViewController.xib文件,向view中添加UIImageView对象,选中该对象,通过Attr ...
- vue项目使用echarts按需引入实现地图动态显示效果时,报错:TypeError: Cannot read property 'dataToPoint' of undefined
vue项目使用echarts按需引入实现地图动态显示效果时,报错:TypeError: Cannot read property 'dataToPoint' of undefined 借鉴了该大神的文 ...
- MySQL 5.7中如何定位DDL被阻塞的问题
在上篇文章<MySQL表结构变更,不可不知的Metadata Lock>中,我们介绍了MDL引入的背景,及基本概念,从“道”的层面知道了什么是MDL.下面就从“术”的层面看看如何定位MDL ...