question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Basic TransitionMotion

See original GitHub issue

I’m trying to animate in/out a single component when mounted, however am struggling to find a basic example that doesn’t animate a list/internal state/etc.

I might be misunderstanding how react-motion works, is this possible in a simple way?


// Transition container
class Transition extends React.Component {
  willLeave() {
    return {opacity: spring(0)}
  }

  willEnter() {
    return {opacity: spring(1)}
  }

  getStyles() {
    return [{key: 'one', style: {opacity: spring(1)} }] 
  }

  render() {
    return (
      <div>
        <TransitionMotion styles={this.getStyles()} willLeave={this.willLeave} willEnter={this.willEnter}>
          {int => <div style={{opacity: int.opacity}}> hello there </div>}
        </TransitionMotion>
      </div>
    )
  }
}



// Main container
export default class Container extends React.Component {
  constructor(){
    super()
    this.state = {
      openCard: false,
    }
  }

  openCard(props) {
    this.setState({ openCard: !this.state.openCard })
  }

  render() {
    return <div> {this.state.openCard ? <Transition/> : null } </div>
    )
  }
}



Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:8
  • Comments:9

github_iconTop GitHub Comments

21reactions
echenleycommented, Feb 23, 2016

I am using it for Route transitions, which is essentially the same thing (swapping out a single component). Here is my full RouteTransition component:

import React from 'react';
import { TransitionMotion, spring } from 'react-motion';

const willEnter = () => ({
  opacity: 0,
  scale: 0.98
});

const willLeave = () => ({
  opacity: spring(0),
  scale: spring(1.02)
});

const getStyles = () => ({
  opacity: spring(1),
  scale: spring(1)
});

const RouteTransition = ({ children: child, pathname }) => (
  <TransitionMotion
    styles={ [{
      key: pathname,
      style: getStyles(),
      data: { child }
    }] }
    willEnter={ willEnter }
    willLeave={ willLeave }
  >
    { (interpolated) =>
      <div>
        { interpolated.map(({ key, style, data }) =>
          <div
            key={ `${key}-transition` }
            style={ {
              ...styles.wrapper,
              opacity: style.opacity,
              transform: `scale(${style.scale})`
            } }
          >
            { data.child }
          </div>
        ) }
      </div>
    }
  </TransitionMotion>
);

var styles = {
  wrapper: {
    position: 'absolute',
    width: '100%'
  }
};

export default RouteTransition;

It is used like this:

<RouteTransition pathname={ location.pathname }>
  { this.props.children /* current route component */ }
</RouteTransition>
7reactions
echenleycommented, Feb 24, 2016

@boyswan My component is guaranteed to have a child, but yours isn’t.

// this array should be empty if there are no children
styles={[{ key: 'key', style: getStyles(), data: children }]} 

You would have to do something like:

styles={ children ? [{ key: 'key', style: getStyles(), data: children }] : [] } 

It’s easier if you’re already dealing with an array. In that case you just use .map:

styles={ items.map(item => ({ key: item.id, style: getStyles(), data: item }) }

Unfortunately in React, this.props.children can be either an array or a single element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple Transition with React Motion | by Zak Risha - Medium
We are going to make a new component, I'm going to call mine Transition . We will also need to import two items...
Read more >
Introduction to React Motion - LogRocket Blog
In this guide, we'll demonstrate how to install the library and share some basic tips to help you build natural-looking, physics-based ...
Read more >
react - <TransitionMotion> - Web Coding Center
<TransitionMotion> helps you to do mounting and unmounting animation. 'Square c' shrinks till disappearance.RESETRUNFULL import React from 'react ...
Read more >
React Motion · GitBook - Tidepool
Tools for even simple CSS3-based animations on mounting and unmounting ... Because its TransitionMotion API suits most of our use cases and because...
Read more >
Doodle Lines Transition Motion Array 2022 || Simple. Easy. And ...
Hey guys in this video we will learn Doodle Lines Transition Motion Array that you can apply in your videos. There are 24...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found