首页 文章

React Motion将输入并且将不会触发

提问于
浏览
1

我正在尝试启动并运行反应运动,但出于某种原因,使用TransitionMotion时,方法 willEnterwillLeave 似乎没有被触发 .

目前我的设置如下:

import React, { Component } from 'react';
import RecommendationItem from '../typeahead/typeahead_recommendation_block';
import { TransitionMotion, spring } from 'react-motion';

const CALIBRATE = { stiffness: 120, damping: 14 };

export default class Recommendations extends Component {

constructor(props) {
  super(props);

  this.willEnter = this.willEnter.bind( this );
  this.willLeave = this.willLeave.bind( this );
  this.getStyles = this.getStyles.bind( this );
}

willEnter() {
  console.log( 'Enter' );
  return {
    maxHeight :0,
    opacity: 0
  }
}

willLeave(){
  console.log( 'Leave' );
  return {
    maxHeight : spring(0, CALIBRATE),
    opacity: spring(0, CALIBRATE)
  }
}

getStyles() {
  return {
    maxHeight: spring(500, CALIBRATE),
    opacity: spring(1, CALIBRATE)
  }
}

render() {
  const {
    showRecommendations
  } = this.props

  if( !showRecommendations ) {
    return null;
  }

  return (
    <div className="typeahead-recommendations">
      <TransitionMotion
        willEnter={ this.willEnter }
        willLeave={ this.willLeave }
        styles={
          Object.keys( this.props.recommendations ).map( ( key, i ) => ({
            key : `${key}-${i}`,
            data : {
              title           : key,
              recommendations : this.props.recommendations[key]
            },
            style : this.getStyles()
          }))
        }>
        { (interpolate) =>
          <div>
            {
              interpolate.map(block => {
                if( block.data.recommendations.length && block.key !== 'productSuggestions' ) {
                  return  <div key={ block.key } style={ block.style }>
                            <RecommendationItem
                              title={ block.data.title }
                              recommendations={ block.data.recommendations } />
                          </div>
                }
              })
            }
          </div>
        }
      </TransitionMotion>
    </div>
  )
}
}

Recommendations.displayName = "Recommendations";

目前,willEnter和willLeave中的console.logs根本就不会触发 . 任何关于为什么会出现这种情况的建议都会受到赞赏

1 回答

  • 0

    也许你正在卸载整个 TransitionMotion 组件 . 您仍然需要将"this.props.recommendations"数据提供给 TransitionMotion .

    例如,如果删除 this.props.recommendations 内部 this.props.recommendations 数据,则过渡动作将保留项目并仍然将其传递给子函数参数 . 所以当你仍然 生产环境 相同的组件时 .

    它将保持插值,同时检查每帧的删除的当前项值 . 一旦删除的项目达到指定的0, TransitionMotion 将永久删除它 .

相关问题