Vla*_*iuk 3 jsx reactjs react-spring
我正在尝试使用 React-spring 安装/卸载组件时创建动画:
import { Transition, animated } from 'react-spring'
...
export class CompName extends PureComponent<CompNamePropsType> {
static defaultProps = {
isExpanded: false,
}
render() {
const {
isExpanded,
...props
} = this.props
return (
<section className={styles.block} {...props}>
<Transition
from={{ opacity: 0 }}
enter={{ opacity: 1 }}
leave={{ opacity: 0 }}>
{isExpanded && (style => (
<animated.div style={{ ...style, background: "orange" }}>
<AnotherComp />
</animated.div>
))}
</Transition>
</section>
)
}
}
Run Code Online (Sandbox Code Playgroud)
但这是行不通的,我收到了一个错误children is not a function。我做错了什么,如何使用react-spring包装器在组件安装/卸载上创建动画?
正确的做法是:
<Transition items={isExpanded} native from enter leave>
{isExpanded => isExpanded && (props => <animated.div style={props} />)}
</Transition>
Run Code Online (Sandbox Code Playgroud)
输入的项目可以是单个项目,就像您的情况一样,然后针对出现的项目进行处理。原因是转换将保留它,即使实际状态发生变化,它仍然可以安全地将元素转换为“旧”状态。
api 的解释如下:react-spring.io/docs/props/transition
| 归档时间: |
|
| 查看次数: |
8538 次 |
| 最近记录: |