反应CSSTransition不过渡

时间:2019-04-07 14:30:59

标签: reactjs react-router react-transition-group

我正在使用react-transition-group,并且代码正在渲染,但是页面没有与动画过渡。如果需要的话,我正在使用来自react-router-dom的Link和NavLink。

我尝试延长超时时间,以查看DOM更改无济于事。假设我的代码一定以某种方式无济于事,我已经多次重构了代码。我已经尝试了react-transition-group-v2,但发现大多数在线资源仍在使用v1,试图按照其指示无效。 Latest example。仍然茫然。

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { ParallaxProvider } from 'react-scroll-parallax';
import { request_page } from "./data/fetch_data.js";
import { TransitionGroup, CSSTransition } from "react-transition-group";

// global components
import Navigation from "./components/Header/Navigation.js";
import Work_With_Us from "./components/Global/Work_With_Us_Button.js";

// routes
import Home from './pages/Home';
import Work from './pages/Work';
import Case_Study from './pages/Case_Study';
import About from './pages/About';


class App extends Component {
    render() {
        return (
            <Router>
                <div className="site-contain">
                    <Navigation />
                    <Work_With_Us />
                    <TransitionGroup>
                        <CSSTransition key={location.key} classNames="page" timeout={30000}>
                            <Switch location={location}>
                                <ParallaxProvider>
                                    <Route exact path='/' component={Home}/>
                                    <Route path='/work/:case_study' component={Case_Study} />
                                    <Route path='/work' component={Work}/>
                                    <Route path='/about' component={About}/>
                                </ParallaxProvider>
                            </Switch>
                        </CSSTransition>
                    </TransitionGroup>
                </div>
            </Router>
        );
    }
}

export default App;

1 个答案:

答案 0 :(得分:1)

这是因为每次路由更新时location.key都会不断变化。

因此,要解决此问题,您可以尝试使用this.props.location.pathname代替this.props.location.key。像ValueForm

工作示例:https://codesandbox.io/s/4RAqrkRkn?view=preview

在GitHub上还有一个问题,其中包含更多示例和实现方式,请检查:https://github.com/ReactTraining/react-router/issues/5279

干杯!