使用React Router v4对React Transition Group v2进行反应

时间:2017-10-12 20:31:36

标签: reactjs animation transitions react-router-v4 react-transition-group

嘿,大家都试图让这个工作几个小时。我试图在路线变化上做一个基本的淡入/淡出。我使用反应路由器v4并对转换组v2做出反应。

我已经在这里阅读了很多其他问题,我可以轻松工作,但是当我点击新页面时,旧页面会立即消失而不是淡出。以下是我的工作内容:

index.js:

    import React from 'react'
    import ReactDOM from 'react-dom'
    import { BrowserRouter } from 'react-router-dom'
    import App from './components/app'


    ReactDOM.render(
        <BrowserRouter>
            <App />
        </BrowserRouter>,
        document.querySelector('#container')
    )

app.js:

    import React, { Component } from 'react'
    import PropTypes from 'prop-types'
    import PageTop from './page-top'
    import { Route, Switch, withRouter } from 'react-router-dom'
    import { TransitionGroup, CSSTransition } from 'react-transition-group'

    import Header from './header'
    import Home from './home/home'
    import Work from './work/work'
    import Services from './services/services'
    import About from './about/about'
    import StartAProject from './start-a-project/start-a-project'

    class App extends Component {
        static propTypes = {
            location: PropTypes.object.isRequired
        }

        constructor(props) {
            super(props)

            this.state = {
                nav: false
            }
        }

        toggleNav() {
            this.setState({
                nav: !this.state.nav
            })
        }

        offNav() {
            if (this.state.nav) {
                this.setState({
                    nav: false
                })
            }
        }

        render() {
            const navActiveClass = this.state.nav ? 'nav-active' : ''
            const { location } = this.props

            return (
                <div className={`app ${navActiveClass}`}>
                    <Header nav={this.state.nav} offNav={this.offNav.bind(this)} toggleNav={this.toggleNav.bind(this)} />

                    <TransitionGroup>
                        <CSSTransition
                            key={location.key}
                            classNames="fade"
                            timeout={{ enter: 500, exit: 500 }}
                            mountOnEnter={true}
                            unmountOnExit={true}
                        >
                            <div>
                                <Switch location={location}>
                                    <Route exact path="/" component={Home} />
                                    <Route path="/work" component={Work} />
                                    <Route path="/services" component={Services} />
                                    <Route path="/about" component={About} />
                                    <Route path="/start-a-project" component={StartAProject} />
                                </Switch>
                            </div>
                        </CSSTransition>
                    </TransitionGroup>
                </div>
            )
        }
    }

    export default withRouter(App)

home.js(所有页面都是这样的):

    import React, { Component } from 'react'
    import PageTop from '../page-top'

    export default class Home extends Component {
        render() {
            return (
                <div className="home-page">
                    <PageTop>
                        <div className="content">
                            <h2>Home</h2>
                        </div>
                    </PageTop>
                    <div>home page test</div>
                </div>
            )
        }
    }

的style.css:

    .fade-enter {
        opacity: 0;
        transition: 0.5s opacity;
    }

    .fade-enter-active {
        opacity: 1;
    }

    .fade-exit {
        opacity: 1;
        transition: 0.5s opacity;
    }

    .fade-exit-active {
        opacity: 0;
    }

header.js

    import React, { Component } from 'react'
    import { Link } from 'react-router-dom'

    export default class Header extends Component {
        constructor(props) {
            super(props)

            this.state = {
                nav: false
            }
        }

        render() {
            return (
                <header className="header padding clear">
                    <h1 className="logo">
                        <Link  to="/" onClick={this.props.offNav}>
                            Name
                        </Link>
                    </h1>

                    <a className="hamburger" onClick={this.props.toggleNav}>
                        <span className="hamburger-bar"></span>
                        <span className="hamburger-bar"></span>
                        <span className="hamburger-bar"></span>
                        <span className="hamburger-bar"></span>
                    </a>

                    <nav className="nav">
                        <ul>
                            <li>
                                <Link to="/work" onClick={this.props.offNav}>Work</Link>
                            </li>
                            <li>
                                <Link onClick={this.props.offNav} to="/services">Services</Link>
                            </li>
                            <li>
                                <Link onClick={this.props.offNav} to="/about">About</Link>
                            </li>
                            <li>
                                <Link onClick={this.props.offNav} to="/start-a-project">Start a Project</Link>
                            </li>
                        </ul>
                    </nav>
                </header>
            )
        }
    }

非常感谢帮助, 谢谢!

0 个答案:

没有答案