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的更多相关文章

  1. redux源码解析(深度解析redux+异步demo)

    redux源码解析 1.首先让我们看看都有哪些内容 2.让我们看看redux的流程图 Store:一个库,保存数据的地方,整个项目只有一个 创建store Redux提供 creatStore 函数来 ...

  2. 我的第一个 react redux demo

    最近学习react redux,先前看过了几本书和一些博客之类的,感觉还不错,比如<深入浅出react和redux>,<React全栈++Redux+Flux+webpack+Bab ...

  3. vuex与redux,我们都一样

    vuex与redux的主要区别: redux:生成的全局数据流是通过每个组件的props逐层传递到各个子组件的,通过@connect装饰器绑定在this.props上面. vuex :生成的全局数据则 ...

  4. Qt+腾讯IM开发笔记(一):腾讯IM介绍、使用和Qt集成腾讯IM-SDK的工程模板Demo

    前言   开发一个支持全国的IM聊天,可以有基本的功能,发送文本.图片.文件等等相关内容.   腾讯IM产品 概述   腾讯即时通信IM是腾讯推出的即时聊天程序,当前时间为2020年3月(腾讯IM的优 ...

  5. FEE Development Essentials

    FEE Development Essentials JS Basic function call() and apply() func1.bind(thisObj,arg1...argn) Cust ...

  6. React 页面间传值的个人总结

    react 组件之间传值的方案有很多,下面是我个人经验的总结 props 来传递值 传值方式: 通过props 获取值 通过props 提供的func去修改值 优点: 不需要任何第三方的组件,纯rea ...

  7. 小程序开发:用Taro搭建框架

    1.node环境 1) 下载 . 官方地址:https://nodejs.org/en/ 或 https://nodejs.org/zh-cn/ 2)安装. 一路next......Install.直 ...

  8. 通过一个demo了解Redux

    TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...

  9. angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”

    曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...

随机推荐

  1. C语言 大小写字母转换

    //凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 方法1: #include<stdio.h> #include<stdlib.h> ...

  2. 强大而灵活的字体图标替代库iconfont

       前言概述 在开发网页制作过程中通常需要一些网页小图标,前端需要PS切图,将单个小图标icon组合成CSS雪碧图过程中,需要一些时间和精力; 如果网页制作中需要的小图标icon有一套css框架库集 ...

  3. 分布式消息中间件rocketmq的原理与实践

    RocketMQ作为阿里开源的一款高性能.高吞吐量的消息中间件,它是怎样来解决这两个问题的?RocketMQ 有哪些关键特性?其实现原理是怎样的? 关键特性以及其实现原理 一.顺序消息 消息有序指的是 ...

  4. CentOS7下使用YUM安装MySQL5.6

    (1)检查系统中是否已安装 MySQL. rpm -qa | grep mysql 返回空值的话,就说明没有安装 MySQL . 注意:在新版本的CentOS7中,默认的数据库已更新为了Mariadb ...

  5. 【足迹C++primer】32、定制操作_2

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/32301839 定制操作_2 完整的big ...

  6. UVA211-The Domino Effect(dfs)

    Problem UVA211-The Domino Effect Accept:536  Submit:2504 Time Limit: 3000 mSec  Problem Description ...

  7. 【转】AJAX请求和普通HTTP请求区别

    两者本质区别: AJAX通xmlHttpRequest象请求服务器服务器接受请求返数据实现刷新交互 普通http请求通httpRequest象请求服务器接受请求返数据需要页面刷新 AJAX请求 普通请 ...

  8. 006_ansible1.9.6版本的安装

    一.Linux的安装 cd /usr/local/src && wget https://pypi.python.org/packages/source/a/ansible/ansib ...

  9. python3 练习题(用函数完成登录注册以及购物车的功能)

    ''' 用函数完成登录注册以及购物车的功能 作业需求: 1,启动程序,用户可选择四个选项:登录,注册,购物,退出. 2,用户注册,用户名不能重复,注册成功之后,用户名密码记录到文件中. 3,用户登录, ...

  10. P2257 YY的GCD--洛谷luogu

    传送门 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 kAc这种傻×必然不 ...