Git Product home page Git Product logo

react-road-on-the-way's People

Contributors

brancelee avatar

Watchers

 avatar

react-road-on-the-way's Issues

React-Redux 中间件applyMiddleware

Redux 中间件applyMiddleware

middleware 是dispatch 一个action 和 action 达到reducer 之间的一个中间层,换句话说是 action 与 store 层的桥梁或者交警,
这个桥梁能够处理异步的行为,等异步结束后,在更新store 层的数据。
先看一个redux 中的action一段代码

// action
export const userLoggedIn = (user) => ({
	type: USER_LOOGGED_IN,
	user
});



export const signUp =(user)=>(dispatch)=>{
    api.user.signUp(user).then(user=>{
        localStorage.bookworm=user.token;
        dispatch(userLoggedIn(user));
    })
}

//reducer
import { USER_LOOGGED_IN, USER_LOOGGED_OUT } from '../types';

export default function user(state = {}, action = {}) {
	switch (action.type) {
		case USER_LOOGGED_IN:
			return action.user;
		case USER_LOOGGED_OUT:
			return {};
		default:
			return state;
	}
}


//store 层
//对于store 储存仓来说 ,RootReducer 是传入给store 新数据 , applyMiddleware(thunk) 为了
//是拦截异步的 action 行为,虽然
//此处的demo 中action 虽为同步事件,但比方说 :
//dispatch(()=>{api.user.signUpAgain}).then((data)=>{dispatch(action)})
//这样我们就可以在一个数据流中组合多个异步的 action 事件了
const store = createStore(RootReducer, composeWithDevTools(applyMiddleware(thunk)));

Ref多个异步的demo
使用这种方式,可以在浏览器渲染时,将所有数据那完整后,再同步渲染页面,这就是使用中间件的好处

React 实现预加载Promise

场景:为解决多张照片预加载,JS实现动画播放,且无画面闪烁问题

同时考察了Primise all 的用法,答应所有的promise 完成后才return 值

本次使用tween.js 实现动画的播放,且防止图片正在加载,出现断贞情况,需预加载动画。

     import ... from ... //图片imgx
     import TWEEN from '@tweenjs/tween.js'
     class PlayAnimation from Component{
     constructor(props){
         this.bgImgs = [img0, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img20, img22, img23, img24, img25, img26, img27, img28, img29, img30, img31, img32, img33, img34, img35, img36, img37, img38, img39];
         this.state={
            imgBgSrc:imgg39;
         }
     }
    
    componentDidMount()=>{
        this.preloadImg(this.bgImgs).then(()=>{
            //播放连帧动画
            this.loopImgBg();
        }).catch(()=>{
        // 加载失败的应变图
            this.setState({img39})
        })
    }
    
    preloadImg=(imgs)=>{
        return Promise.all(imgs.map(src=>{
            return new Promise((resolve,reject)=>{
                var img=new Image();
                img.src=src;
                img.onload=resolve;
                img.onerror=reject;
            })
        }))
    }
    
    loopImgBg=()=>{
        const that=this
        const imgLength=this.bgImgs.length;
        this.tween=new TWEEN.Tween({x:0})
            .to({x:imgLength*10 -1},2000)
            .easing(TWEEN.Easing.Linear.None)
            .onUpdate(coords=>{
                const {x}=coords
                const loopIndex =Math.floor(x/10;
                const imgBgSrc= that.bgImgs[loopIndex];
                this.setState({imgBgSrc}),
            })
            .start();
    }
    
    render(){
        const {imgBgSrc}=this.state
        return (
            <div>
                <img src={imgBgSrc} alt="Loading ..."></img>
            </div>
        )
    }
}
export default PlayAnimation;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.