pmndrs / react-spring.io

✌️ A spring physics based React animation library
https://www.react-spring.io
56 stars 45 forks source link

[Request] Add integration example with Reach Router #61

Open Oba-One opened 5 years ago

Oba-One commented 5 years ago

Hi! @drcmda I was wondering if you have examples with Reach Router. Here's what I have so far, appreciate the help!

import React, { useState } from 'react';
import { Router, Location } from '@reach/router';
import { useTransition, animated } from 'react-spring';
import { Container } from '../global';
import styled from 'styled-components';

const AnimatedContainer = styled(animated(Container))``;

const AnimatedRouter = ({ children }) => {
    const [index, set] = useState(0);
    const transitions = useTransition(index, p => p, {
        from: { opacity: 0, transform: 'translate3d(100%,0,0)' },
        enter: { opacity: 1, transform: 'translate3d(0%,0,0)' },
        leave: { opacity: 0, transform: 'translate3d(-50%,0,0)' },
    });

    return (
        <Location>
            {({ location }) => (
                <AnimatedContainer key={location.key}>
                    <Router location={location}>{children}</Router>
                </AnimatedContainer>
            )}
        </Location>
    );
};

export default AnimatedRouter;
Sampite commented 5 years ago

Hey @Oba-One ! I also use react-spring and react-router together, there is a nice example that I used as a base and tailored for my needs at https://www.react-spring.io/docs/hooks/use-transition Demos section at the bottom. It is currently referring to this sandbox

Hope it helps.

Oba-One commented 5 years ago

@Sampite I' using reach router not react router. Do you know any examples for reach?

th3oxen commented 4 years ago

@Oba-One here's a sandbox using reach router. However there's few things that make the transition not working as expected:

Edit: By reading react router documentation I found out that

The useLocation hook returns the location object that represents the current URL. You can think about it like a useState that returns a new location whenever the URL changes.

So I guess that what I am doing with setRoute mimick what they're doing with useLocation

Also is worth noticing that in react router, if you put anything in between the <Switch> and its <Route>s, it will render the current location immediately, just like reach router.

<Switch location={location}>
    <div> // This won't work
        <Route path="/" exact component={ComponentA} />
        <Route path="/b" component={ComponentB} />
    </div>
</Switch>
joshuaellis commented 3 years ago

Moving to react-spring.io where this should probably live.