我正在 react 组件中创建淡出警告/错误消息(引导样式(,并且淡出时间遇到了一些问题。
到目前为止,我的淡出工作正常,这就是现在完成的方式:
import React, { Component } from 'react'
import classnames from 'classnames'
class AlertMessage extends Component {
state = ({ autoHide: false })
componentDidMount(){
const { autoHide } = this.props
if (autoHide && !this.state.hasClosed) {
setTimeout(() => {
this.setState({ autoHide: true, hasClosed: true })
}, 5000)
}
}
render() {
const { error, info, success, warning, text } = this.props
const classNames = {
'error': error,
'info': info,
'success': success,
'warning': warning,
'alert-hidden': this.state.autoHide
}
return (
<div className={classnames("alert-message", classNames)}>
{text}
</div>
)
}
}
export default AlertMessage
现在,我想删除 setTimeout 和状态,使其成为功能性无状态组件。我的问题是我的样式中的过渡延迟似乎不起作用,恐怕这与 classNames 如何将类应用于组件有关。
这是我的风格:
.alert-message{
overflow-y: hidden;
opacity: 1;
max-height: 80px;
transition-property: all 450ms;
transition-duration: 450ms;
transition-timing-function: cubic-bezier(0, 1, 0.5, 1);
transition-delay: 5000ms;
谢谢
蓝图框架的Toast组件使用超时功能来处理这个问题。您可以从其源代码中提取相同的功能
据我所知,您目前无法仅通过 CSS 在 react 中实现动画。