寫了一個基於React+Redux的仿Github進度條

來源:互聯網
上載者:User

標籤:

曾經實現過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進度條

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.