How to implement react-transition-group?

0

I'm trying to implement react-transition-group in my project using the new react-router , but I'm not making it work and I do not find very clear documentation, or at least I can understand, if someone can give me a hand in seeing what I'm doing wrong will be very grateful:)

Here my component

import React from 'react';
import { Route, Switch, withRouter } from 'react-router-dom';
import { TransitionGroup, Transition } from 'react-transition-group';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import Landing from '../../pages/Landing/Landing';
import Example from '../../pages/Example/Example.js';
import { AsyncNotFound, AsyncAbout } from '../../util/async-section-handler';

import './Pages.css';

import checkProps from '../../util/check-props';

const duration = 300;

const defaultStyle = {
  transition: 'opacity ${duration}ms ease-in-out',
  opacity: 0
};

const transitionStyles = {
  entering: { opacity: 0 },
  entered: { opacity: 1 }
};

class Pages extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = { in: false };
  }

  // toggleEnterState = () => {};

  componentDidMount() {
    this.setState({ in: true });
  }

  componentWillReceiveProps(nextProps) {}

  render() {
    return (
      <main className={classnames('Pages', this.props.className)} role="main">
        <TransitionGroup>
          <Transition in={this.state.in} timeout={duration}>
            {state => (
              <div
                style={{
                  ...defaultStyle,
                  ...transitionStyles[state]
                }}
              >
                <Switch location={this.props.location}>
                  <Route exact={true} path="/" component={Landing} />
                  <Route exact={true} path="/about" component={AsyncAbout} />
                  <Route exact={true} path="/example" component={Example} />
                  <Route component={AsyncNotFound} />
                </Switch>
              </div>
            )}
          </Transition>
        </TransitionGroup>
      </main>
    );
  }
}

Pages.propTypes = checkProps({
  className: PropTypes.string
});

Pages.defaultProps = {
  className: ''
};

export default withRouter(Pages);
    
asked by Santiago D'Antuoni 16.05.2018 в 16:38
source

0 answers