reducer在react使用
编写store.js 小state
reducer 怎么来 纯函数
state+action 生成新的state
actions type
return{
}
state
action === setstate(()=>{})
reducer.js 生成新的state 计算
action.js 修改state (数据初始化,和操作方法)
_actionsType.js
页面引入
import {connent}from 'react-redux'
1.src 下面有一个大的store>store.js
import {createStore, combineReducers,applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import {reducer as twoReducer} from '../views/TwoRedux/_index.js';
import {reducer as downupReducer} from '../views/Xiala/_index';
import {reducer as wueRling} from '../views/expers/_index';
import {reducer as shuJu} from '../views/shixian/_index';
const reducer = combineReducers({
two:twoReducer,
downup:downupReducer,
wuer:wueRling,
shixia:shuJu,
})
export default createStore(reducer,applyMiddleware(thunk));

2.在Route.js注入
import React from "react";
import { BrowserRouter, HashRouter } from "react-router-dom";
import App from "./App.js";
import { Provider } from "react-redux";
import Store from "./store/store";
const Router = () => (
<BrowserRouter>
<Provider store={Store} >
<App />
</Provider>
</BrowserRouter>
);
export default Router;

3.在src>view>创建action.js ActionTye.js Reducer.js文件
Action.js
import * as ActionType from './ActionType'
import Unit from '../../../Un'
// 数据
export const getData = (text,data={},prevData)=>{
// console.log(prevData)
// data.data.push.apply(data.data,prevData);
// data.data = data.data.concat(prevData);
data.data = [...prevData,...data.data]
return{
type:ActionType.LISTDATA,
text:text,
pageData:data
}
}
// 方法
// 首次加载 或 刷新 时 都是第一页
// res.data 10 [] 10
// for X
// ...
// [...prevData,...res.data.data] 10 20
export const getFn = (text,ajax,dispatch,prevData)=>{
return{
type:ActionType.GETPDD,
text:text,
ajaxFn:Unit.getApi(ajax).then((res)=>{
// console.log(res.data)
dispatch(getData('发送请求',res.data,prevData))
})
}
}

ActionTye.js
export const LISTDATA="TODO_LISTDATA";
export const GETPDD="TODO_GETPDD";

Reducer.js
import * as ActionType from './ActionType.js';
export default (state={},action)=>{
switch (action.type) {
case ActionType.LISTDATA:
return {
action:{
type:action.type,
text:action.text
},
pddApi:action.pageData
}
default:
return state;
}
}

4.在src>view里面创建app.jsx 和_index.js
_index.js
import * as actions from './_store/Action';
import reducer from './_store/Reducer';
export {actions,reducer};

app.jsx
import React, { Component } from 'react';
import {connect} from 'react-redux';
import {actions} from './_index';
//import ReactPullLoad, { STATS } from "react-pullload";
//import './ReactPullLoad.scss'
class View extends Component {
constructor(props){
super(props);
this.state={
ajaxCfg:{
url:'/home/mediareports',
cfg:{
page_number:'1',
page_size:'10',
},
headers:{
}
},
hasMore: true,
action: STATS.init,
index: 6,
page:1
}
}
handleAction = action => {
// console.info(action, this.state.action, action === this.state.action);
//new action must do not equel to old action
if (action === this.state.action) {
return false;
}
if (action === STATS.refreshing) {
//刷新
this.handRefreshing();
} else if (action === STATS.loading) {
//加载更多
this.handLoadMore();
} else {
//DO NOT modify below code
this.setState({
action: action
});
}
}
handRefreshing = () => {
if (STATS.refreshing === this.state.action) {
return false;
}
setTimeout(() => {
//refreshing complete
this.setState({
hasMore: true,
action: STATS.refreshed,
index: 6
});
// console.log('刷新');
this.init();
}, 3000);
this.setState({
action: STATS.refreshing
});
}
handLoadMore = () => {
const { down } = this.props;
if (STATS.loading === this.state.action) {
return false;
}
//无更多内容则不执行后面逻辑
if (!this.state.hasMore) {
return;
}
setTimeout(() => {
if (this.state.index === 0) {
this.setState({
action: STATS.reset,
hasMore: false
});
} else {
this.setState({
action: STATS.reset,
index: this.state.index - 1
});
}
console.log('加载更多');
this.setState((state,props)=>{
page:state.page++
})
this.getPddFn(this.state.page,down.pddApi.data)
}, 3000);
this.setState({
action: STATS.loading
});
}
init(){
// 代码初始化
this.getPddFn(1,[])
}
getPddFn(page,prevData){
const { getPddFn } = this.props;
const { ajaxCfg } = this.state;
ajaxCfg.cfg.page_number = page;
getPddFn('首次启用',ajaxCfg,prevData)
}
componentDidMount(){
this.getPddFn(1,[])
}
lists(){
const { down } = this.props;
return down.pddApi.data.map((val,index)=>{
return(
<li key={val.id}>
{index}-----{val.main_title}
</li>
)
})
}
render(){
const { down } = this.props;
console.log(down)
const { hasMore } = this.state;
// console.log(down)
return(
<React.Fragment>
<ReactPullLoad
downEnough={150}
action={this.state.action}
handleAction={this.handleAction}
hasMore={hasMore}
distanceBottom={1000}
>
<div className="div1">111</div>
<div className="div1">111</div>
<div className="div1">111</div>
<div className="div1">111</div>
<div className="div1">111</div>
<div className="div1">111</div>
<div className="div1">111</div>
{
down.pddApi
?
<ul>{this.lists()}</ul>
:
''
}
</ReactPullLoad>
</React.Fragment>
)
}
}
const mapStateToProps = (state)=> {
return {
down:state.downup
}
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
getPddFn:(text,ajaxcfg,prevData)=>
dispatch(actions.getFn(
text,
ajaxcfg,
dispatch,
prevData
))
}
};
export default connect(mapStateToProps,mapDispatchToProps)(View);

reducer在react使用的更多相关文章
- React 和 Redux理解
学习React有一段时间了,但对于Redux却不是那么理解.网上看了一些文章,现在把对Redux的理解总结如下 从需求出发,看看使用React需要什么 1. React有props和state pro ...
- React内容
React Fiber 16版本 registerServiceWorker 的作用 PWA progressive web application 写手机app应用 在断网的情况下,第二 ...
- react 结合gitte 创建项目(详情步骤)
创建项目第一步 基本搭建 在创建之前,需要有一个git 仓库,我们要把项目搭建到git 中 目录介绍 cd 到某个盘 mkdir workspace 创建workspace文件夹 cd works ...
- react 创建项目 sass router redux
创建项目第一步 基本搭建 在创建之前,需要有一个git 仓库,我们要把项目搭建到git 中 目录介绍 cd 到某个盘 mkdir workspace 创建workspace文件夹 cd works ...
- [开源]React/Vue通用的状态管理框架,不好用你来打我👀
为了防止被打,有请"燕双鹰"镇楼️♀️️️...o... 话说新冠3年,"状态管理框架"豪杰并起.群雄逐鹿,ReduxToolkit.Mobx.Vuex. ...
- redux相关专业名词及函数提要
redux: 用来管理react app 状态(state)的一个架构. store: 通过createStore()创建,用来存放state,与react app是完全分离的.createStore ...
- react+redux状态管理实现排序 合并多个reducer文件
这个demo只有一个reducer 所以合并reducer这个demo用不到 ,但是我写出来这样大家以后可以用到,很好用,管理多个reducer,因为只要用到redux就不会只有一个reducer所以 ...
- [React] How to use a setState Updater Function with a Reducer Pattern
In this lesson we'll walk through setting up an updater function that can receive an action argument ...
- React学习(2)——action,reducer
action creator 是一个函数,格式如下: var actionCreator = function() { // 构建一个 action 并返回它 return { type: 'AN_A ...
随机推荐
- 联盛德 HLK-W806 (四): 软件SPI和硬件SPI驱动ST7735液晶LCD
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- c# Quartzs定时器的简单使用
使用背景: 首先想到倒计时,定时任务.大家想到的肯定就是定时器.那么定时器在web和winfrom程序中有着很大的作用.那在服务器端有没有像定时器一样的存在呢. 有了这些需求,我找到了第三方的组件 Q ...
- python并行计算之mpi4py的安装与基本使用
技术背景 在之前的博客中我们介绍过concurrent等python多进程任务的方案,而之所以我们又在考虑MPI等方案来实现python并行计算的原因,其实是将python的计算任务与并行计算的任务调 ...
- 洛谷 P6383 -『MdOI R2』Resurrection(DP)
洛谷题面传送门 高速公路上正是补 blog 的时候,难道不是吗/doge,难不成逆在高速公路上写题/jy 首先形成的图显然是连通图并且有 \(n-1\) 条边.故形成的图是一棵树. 我们考虑什么样的树 ...
- 洛谷 P4062 - [Code+#1]Yazid 的新生舞会(权值线段树)
题面传送门 题意: 给出一个序列 \(a\),求 \(a\) 有多少个子区间 \([l,r]\),满足这个区间中出现次数最多的数出现次数 \(>\dfrac{r-l+1}{2}\) \(1 \l ...
- 关于 n 个 [0,1] 的随机变量第 k 小的期望值
今天做到一道题,感觉里面一个结论有点意思,就到网上扒了篇证明(bushi)下来了. 知乎回答习惯,先抛结论,再给证明(大雾 结论:对于 \(n\) 个取值范围为 \([0,1]\) 的随机变量 \(x ...
- A Child's History of England.32
And so, in darkness and in prison, many years, he thought of all his past life, of the time he had w ...
- Linux之sftp服务
Linux之sftp服务 一.sftp介绍转自:[1]Linux如何开启SFTP https://www.cnblogs.com/xuliangxing/p/7120205.htmlSFTP是Secu ...
- haproxy动态增减主机与keepalived高级应用
一:本文将详细介绍haproxy的配置使用以及高级功能的使用,比如通过haproxy进行动态添加删除负载集群中的后端web服务器的指定主机,另外将详细介绍keepalived的详细配置方法.配置实例及 ...
- springboot+vue集成mavon-editor,开发在线文档知识库
先睹为快,来看下效果: 技术选型 SpringBoot.Spring Security.Oauth2.Vue-element-admin 集成mavon-editor编辑器 安装 mavon-edit ...