使用React-Motion(https://github.com/chenglou/react-motion),我的表现非常糟糕 . 我正在设置从 0
到 260
的表格行的下拉列表的高度 .
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秒(这是太长时间)的范围内渲染所有这些时,都会记录高度:
也许我误读了文档中的某些内容,但有没有办法避免动画滞后?
1 回答
您需要将过渡样式应用于
div
并在其中呈现实现shouldComponentUpdate
的组件(例如,使用React.PureComponent
)以防止在不需要时重新呈现它 .并且
MyComponent
可能类似于class MyComponent extends React.PureComponent
或使用来自recompose的pure
之类的HOC . 这样MyComponent
只会在道具更改时更新 .