首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React-在每个动画上进行运动渲染

React-在每个动画上进行运动渲染
EN

Stack Overflow用户
提问于 2017-07-20 08:04:41
回答 1查看 647关注 0票数 0

我使用React-Motion (https://github.com/chenglou/react-motion)得到了非常糟糕的性能。我将从0260的表格行的下拉列表的高度动画化。

代码语言:javascript
复制
constructor() {
    this.state = {
        opened: false
    }

    this.handleRowClick = this.handleRowClick.bind(this)
}

handleRowClick() {
    this.setState({
        opened: !this.state.opened
    })
}

render() {
    <Motion style={{height: spring(this.state.opened ? 260 : 0, {stiffness: 140, damping: 30})}}>
        {(height) => 
            <div onClick={this.handleRowClick}>
                <div style={height}>
                    ...stuff goes here
                </div>
            </div>
        }
    </Motion>
}

动画按预期工作,但每次记录高度时,它都会在大约5秒的时间内渲染所有这些内容(这太长了):

也许我在文档中读错了什么,但是有没有办法避免动画的滞后呢?

EN

回答 1

Stack Overflow用户

发布于 2017-09-19 17:44:58

您需要将转换样式应用于div,并在其中呈现一个实现shouldComponentUpdate的组件(例如,.使用React.PureComponent),以防止它在不需要时重新呈现。

代码语言:javascript
复制
render () {
  <Motion 
    style={{
      height: spring(
        this.state.opened ? 260 : 0, 
        { stiffness: 140, damping: 30 }
      )
    }}
  >
    {(height) => 
      <div style={height}>
        <YourComponent />
      </div>
    }
  </Motion>
}

MyComponent可以是像class MyComponent extends React.PureComponent这样的东西,也可以使用像recomposepure这样的HOC。这样,MyComponent只会在它的道具发生变化时进行更新。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45203005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档