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. Java抽象类(Abstract Class)与接口(Interface)区别

    抽象类与接口比较 抽象类跟接口类似,都不能实例化,可能包含不需实现方法或已实现的方法. 抽象类可以定义一些不是静态或常量的字段,定义 public, protected, private访问级别的具体 ...

  2. human pose estimation

    2D Pose estimation主要面临的困难:遮挡.复杂背景.光照.真实世界的复杂姿态.人的尺度不一.拍摄角度不固定等. 单人姿态估计 传统方法:基于Pictorial Structures, ...

  3. python 守护进程、同步锁、信号量、事件、进程通信Queue

    一.守护进程 1.主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic processes ...

  4. xss攻击(跨站脚本)

    原理跨站脚本(Cross site script,简称xss)是一种“HTML注入”,由于攻击的脚本多数时候是跨域的,所以称之为“跨域脚本”. 我们常常听到“注入”(Injection),如SQL注入 ...

  5. 指数加权移动平均法(EWMA)

    ** 本文内容来自于吴恩达深度学习公开课 1.概述 加权移动平均法,是对观察值分别给予不同的权数,按不同权数求得移动平均值,并以最后的移动平均值为基础,确定预测值的方法.采用加权移动平均法,是因为观察 ...

  6. Linux shell判断文件和文件夹是否存在(转发)

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  7. iframe 加载闪过白块问题

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 在使用iframe时,iframe背景为白块,刷新时也会闪过白块.如果刷新时间长,就会 ...

  8. vue.js 官网及组件网站记录

    官网 https://cn.vuejs.org/v2/guide/ 饿了么组件: http://element-cn.eleme.io/#/zh-CN/component/tree 异步请求框架插件 ...

  9. Python:Day15 函数

    函数参数补充: 还可以这样传参: def f(*args): print(args) f(*[1,3,4,5]) #输出结果:(1, 3, 4, 5) 注意这是一个元组 def f2(**kwargs ...

  10. 【angularjs】使用angular搭建项目,获取dom元素

    方法一:需要引入jq,否则会报angularJS1 Error: [jqLite:nosel](不建议使用) <div id="testID" class="tes ...