0

I am trying to animate adding a new comment in my React + Redux app with react-motion.

class Comments extends Component {
    getDefaultStyles() {
        const { comments } = this.props;
        return comments.map((c, i) => {
            return {
                key: "" + i,
                style: { top: -50, opacity: 0.2 },
                data: c
            }
        });
    }
    getStyles() {
        const { comments } = this.props;
        return comments.map((c, i) => {
            return {
                key: "" + i,
                style: {
                    top: spring(0, presets.gentle),
          opacity: spring(1, presets.gentle)
                },
                data: c
            }
        })
    }
    willEnter() {
        return { top: -50, opacity: 0.2 }
    }
    render() {
        let { comments } = this.props;
        return (
            <div>
                <TransitionMotion defaultStyles={this.getDefaultStyles()} styles={this.getStyles()} willEnter={this.willEnter}>
                {styles =>
                    <div>
                    {
                        styles.map((c) => {
                            return (
                                <Comment key={c.key} comment={c.data} style={{...c.style, overflow: 'hidden'}} />
                            )
                        })
                    }
                    </div>
                }
                </TransitionMotion>
            </div>
        )
    }
}

Then the style is passed to the first div in Comment component.

While loading the comments the animation is OK. But user add a comment, and then the fetchComments method is called to fetch all the comments, the animation does not occur. Is this something to do with redux? I am passing my comment using mapStateToProps and connect.

mdmb
  • 4,833
  • 7
  • 42
  • 90

1 Answers1

0

The problem was with the key. The animation was occuring, but at the bottom of the comments, as mapping through them assigned them a key based on index in the array. When I changed the key to contain comment.id number it started working properly!

mdmb
  • 4,833
  • 7
  • 42
  • 90