Giter Club home page Giter Club logo

react-router-v4-transition's Introduction

React Router Transition

Build Status Coverage Status npm version

Transitions for React Router v4. The API is composed of a component, TransitionSwitch, that should be used as the Switch component from react-router v4 to switch from a route to another one with a transition. That transition can be any action you need to do between routes, like animation, or fetching data.

API Description

1) The component:

<TransitionSwitch parallel={false}>
    <Route exact path="/">
        <Transition>home path</Transition>
    </Route>
    <Route path="/otherPath">
        <Transition>other path</Transition>
    </Route>
    <Route path="/">
        <Transition>other home</Transition>
    </Route>
    <Route path="/anotherPath">
        <Transition>another path</Transition>
    </Route>
</TransitionSwitch>

TransitionSwitch allows you to perform transitions on route change. Given its name, it works like the router v4 Switch. It means that only one route will be visible at all times. Except if parallel is set to true, which means that the entering transition won't wait for the leaving transition to be finished. NB: parallel may be renamed in the future.

2) The transitions:

Like a switch, the children must be Route elements. The children of these route elements will be given hooks to perform the transition. These hooks are :

class Transition extends React.Component {
    
    componentWillAppear(callback) {
        //do something when the component will appear
        
        callback();
    }
    
    componentDidAppear() {
        //do something when the component appeared
    }
    
    componentWillEnter(callback) {
        //do something when the component will enter
        
        callback();
    }
    
    componentDidEnter() {
        //do something when the component entered
    }
    
    componentWillLeave(callback) {
        //do something when the component will leave
                
        callback();
    }
    
    componentDidLeave() {
        //do something when the component has left    
    }
    
}

The callbacks must be called after the transition is complete, in the case of animation, a good place is in the callback provided by the animation library. The interface is very much the same as react-trasition-group v1. This means that componentWillAppear is called for the first time when the TransitionSwitch is mounted.

Sample App

In case you want to quickly try it, there's a webpack setup and very rough sample app. In order to build it you should run :

npm run build:example
npm run start:server

The app will be running at localhost:8080, the build command watches for changes in case you want to play with it, the sources are located in src/example.

react-router-v4-transition's People

Contributors

aboeglin avatar

Watchers

 avatar

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.