曾经实现过Angular版,这次感觉用了高大上的React却写了更多的代码,需要的配置也更多了,有利有弊吧。

但这个“导航条问题”很有意思,涉及到在Redux中写timer,其实我很困惑,到底如何完美模拟用户的页面加载,

貌似浏览器并没有提供进度API,只能以这样拙劣的方式进行模拟,有兴趣的朋友不妨与我交流。

代码附上:

LoadingBar.jsx

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux'; export class LoadingBar extends Component {
constructor(props) {
super(props); this.state = {
timer: null,
style: {
display: 'none',
position: 'absolute',
width: '0%',
height: '3px',
backgroundColor: 'blue',
transition: 'width 400ms ease-out, height 400ms linear'
}
};
} componentWillReceiveProps(nextProps) {
if (nextProps.status) {
let progress = 0;
this.setState({
style: Object.assign({}, this.state.style, {
width: '0%'
})
}); let timer = setInterval(() => {
if (progress <= (100 - nextProps.step)) {
this.setState({
style: Object.assign({}, this.state.style, {
width: `${progress += nextProps.step}%`,
display: 'block'
})
});
}
}, nextProps.speed); this.setState({
timer: timer
});
} else {
clearInterval(this.state.timer); this.setState({
timer: null,
style: Object.assign({}, this.state.style, {
width: '100%',
display: 'none'
})
});
}
} render() {
return (
<div>
<div style={this.state.style} className={this.props.className}></div>
<div style={{ display: 'table', clear: 'both' }}></div>
</div>
)
}
} LoadingBar.propTypes = {
className: PropTypes.string,
speed: PropTypes.number,
step: PropTypes.number,
status: PropTypes.bool,
} function mapStateToProps(state) {
return {...state.loading};
} export default connect(mapStateToProps)(LoadingBar)

App.jsx

import LoadingBar from 'LoadingBar';

const App = ({children}) => {
return (
<div>
<LoadingBar speed={5} step={2} />
{children}
</div>
);
}; App.propTypes = {
children: PropTypes.object
}; export default App;

loadingReducer.js

export default function loading(
state = {
status: false
},
action = {}
) {
switch (action.type) {
case 'SHOW_LOADING':
return Object.assign({}, state, {
status: true,
});
case 'HIDE_LOADING':
return Object.assign({}, state, {
status: false,
});
default:
return state
}
}

loadingActions.js

export function show() {
return { type: 'SHOW_LOADING' }
} export function hide() {
return { type: 'HIDE_LOADING' }
}

loadingMiddleware.js

import { show, hide } from './loadingActions';

const defaultTypeSuffixes = ['REQUEST', 'SUCCESS', 'FAILURE']

export default function loadingBarMiddleware(config = {}) {
const typeSuffixes = config.typeSuffixes || defaultTypeSuffixes; return ({ dispatch }) => next => action => {
next(action); if (action.type === undefined) {
return;
} const [PENDING, FULFILLED, REJECTED] = typeSuffixes; const isPending = `_${PENDING}`;
const isFulfilled = `_${FULFILLED}`;
const isRejected = `_${REJECTED}`; if (action.type.indexOf(isPending) !== -1) {
dispatch(show());
} else if (action.type.indexOf(isFulfilled) !== -1 || action.type.indexOf(isRejected) !== -1) {
dispatch(hide());
}
}
}

配置Store

import { createStore, applyMiddleware } from 'redux';
import { loadingMiddleware } from 'loadingMiddleware';
import rootReducer from './reducers'; const store = createStore(
rootReducer,
applyMiddleware(loadingMiddleware())
)

配置RootReducer

import { combineReducers } from 'redux';
import { loadingReducer } from './loadingReducer'; const reducer = combineReducers({
loading: loadingReducer,
});

写了一个基于React+Redux的仿Github进度条的更多相关文章

  1. 实例讲解基于 React+Redux 的前端开发流程

    原文地址:https://segmentfault.com/a/1190000005356568 前言:在当下的前端界,react 和 redux 发展得如火如荼,react 在 github 的 s ...

  2. RSuite 一个基于 React.js 的 Web 组件库

    RSuite http://rsuite.github.io RSuite 是一个基于 React.js 开发的 Web 组件库,参考 Bootstrap 设计,提供其中常用组件,支持响应式布局. 我 ...

  3. 快速搭建一个基于react的项目

    最近在学习react,快速搭建一个基于react的项目 1.创建一个放项目文件夹,用编辑器打开 2.打开集成终端输入命令: npm install -g create-react-app 3. cre ...

  4. 基于React,Redux以及wilddog的聊天室简单实现

    本文主要是使用ReactJs和Redux来实现一个聊天功能的页面,页面极其简单.使用React时间不长,还是个noob,有不对之处欢迎大家吐槽指正. 还要指出这里没有使用到websocket等技术来实 ...

  5. 一个基于React整套技术栈+Node.js的前端页面制作工具

    pagemaker是一个前端页面制作工具,方便产品,运营和视觉的同学迅速开发简单的前端页面,从而可以解放前端同学的工作量.此项目创意来自网易乐得内部项目nfop中的pagemaker项目.原来项目的前 ...

  6. 基于Vue的事件响应式进度条组件

    写在前面 找了很多Vue 进度条组件!,都不包含拖拽和点击事件,input range倒是原生包含input和change事件,但是直接基于input range做进度条的话,样式部分需要做大量调整和 ...

  7. iOS适配UIViewView/WKWebView,H5生成长图,仿微信进度条

    前段时间撸代码猥琐发育的时候,设计师老王给了张截图某宝APP上一个生成长图分享的功能,正好公司有这个需求,于是在立马开始操练起来!在万能的度娘上搜集整理资料后发现很多文章介绍的方案对WKWebView ...

  8. mk-js,一个基于react、nodejs的全栈框架

    前言 在这个前端技术爆炸的时代,不自己写套开源框架出门都不好意思跟别人说自己搞前端.去年年初接触的react,16年7月份在github开源了一套针对react.redux探索的项目,近期和伙伴们一起 ...

  9. mk框架,一个基于react、nodejs全栈框架

    在这个前端技术爆炸的时代,不自己写套开源框架出门都不好意思跟别人打招呼,作为一个前端领域的小学生,去年年初接触了react,之后一发不可收拾爱上了它,近期重构了自己去年开源的一个项目,废话到此结束句号 ...

随机推荐

  1. JS原型和继承

    //所有的函数都有一个prototype属性 function aa() { } console.info(aa.prototype); //这个prototype属性引用了一个对象,即原型,初始化时 ...

  2. ToolStrip控件在窗体没有焦点的情况下,需要单击二次才能够激发事件的解决办法

    protected override void WndProc(ref Message m) { if (m.Msg == 0x210) { Control control = Control.Fro ...

  3. MySQL的btree索引和hash索引的区别

    Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...

  4. logistic公式形式的由来,从广义线性回归说起

    普通线性回归的形式为:(之所以这么写是因为的线性才是线性的所指) 线性回归模型有一下以下几个特征: 1. 2.x,y 通常取值连续 3.y的分布为正态分布或接近正态. 广义线性模型进行了如下推广: 1 ...

  5. C6000代码层面优化(一)

    2014年8月7日,看了一片很长见识的博文,关于DSP如何优化的,有一个问题没有搞通,“百度”一下关键字,居然搜查了一模一样的博文N片,现在也搞不懂这篇博文的原创作者是谁了.反正我感觉直接转摘过去,要 ...

  6. 15 个有趣的 JavaScript 与 CSS 库

    原文转载:http://www.codeceo.com/article/15-interesting-js-css-framework.html 1. Wing Wing 是一个微型(压缩后仅有4KB ...

  7. Java中的值传递和引用传递

    这几天一直再纠结这个问题,今天看了这篇文章有点思路了,这跟C++里函数参数为引用.指针还是有很大区别. 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里 ...

  8. libcurl教程

    名称 libcurl 的编程教程 目标 本文档介绍使用libcurl编程的一般原则和一些基本方法.本文主要是介绍 c 语言的调用接口,同时也可能很好的适用于其他类 c 语言的接口. 跨平台的可移植代码 ...

  9. Ajax1

    一.Ajax是什么? 全称"Asynchronous JavaScript and XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJA ...

  10. Spring - constructor-arg和property

    1.说明 constructor-arg:通过构造函数注入.    property:通过setter对应的方法注入. 2.constructor-arg的使用示例 (1).Model代码: 1 2 ...