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 ...
随机推荐
- Socket引子
=== ''' Socket网络编程: --应用层:http smtp dns ftp ssh snmp dhcp... 无论协议是什么本质上都是数据交换,总结为两种方式:收和发 --传输层(端口Po ...
- Linux 与 Unix 到底有什么不同?区别在哪?
Linux 与 Unix 到底有什么不同?区别在哪? 如果你是一名20多岁或30多岁的软件开发人员,那么你已成长在一个由Linux主导的世界中.数十年来,它一直是数据中心的重要参与者,尽管很难找到明确 ...
- [Android] ScrollView can host only one direct child
android 采用ScrollView布局时出现异常:ScrollView can host only one direct child.主要是ScrollView内部只能有一个子元素,即不能并列两 ...
- 【vue】vue +element 搭建及开发中项目中,遇到的错误提示
1. import Layout from '@/views/layout/Layout'; export default [ { // 配置路由,当路径为'/activePublic',使用组件ac ...
- 一、Oracle 安装
一.oracle的安装和链接1.oracle数据库的后台服务: a.Oracle11ghomeTNSListener:数据库服务器的监听程序,负责监听客户端的链接请求 b.OracleServiceO ...
- IndentationError 这个错误是缩进的问题
1.自定义库导入时,提示这个错误,是缩进的问题
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 【Windows】+ windows下在某一文件夹下按“shift+鼠标右键”打开CMD窗口
前言:没更改之前是打开的powershell窗口,超不习惯 新建一个txt文件,然后复制以下代码,然后保存为OpenCmdHere.reg 格式,然后双击即可 Windows Registry Edi ...
- Python分析微信好友性别比例和省份城市分布比例
如需转发请注明:小婷儿的博客:https://www.cnblogs.com/xxtalhr/p/10642241.html 一.安装模块 pip install itchat pip install ...
- Luogu P1776 宝物筛选_NOI导刊2010提高(02)(多重背包模版)
传送门 多重背包板子题, 多重背包就是每种东西有好几个,可以把它拆分成一个一个的01背包 优化:二进制拆分(拆成1+2+4+8+16+...) 比如18=1+2+4+8+3,可以证明18以内的任何数都 ...