我可以使用带有React内联样式的react-addons-css-transition-group中的ReactCSSTransitionGroup吗?如果是这样,怎么办?
我正在处理的组件:
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
import { removeNotification } from '../actionCreators'
import Notification from './Notification'
const mapStateToProps = state => ({
notifications: state.notifications
})
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators({ removeNotification }, dispatch)
})
class Notifications extends Component {
render() {
var style = {
position: 'fixed',
top: 20,
right: 20,
width: 300,
zIndex: 99
}
var tstyle = {
'notification-enter': {
visibility: 'hidden',
transform: 'translate3d(100%,0,0)'
},
'notification-leave': {
visibility: 'visible',
transform: 'translate3d(0,0,0)'
},
'notification-enter-notification-enter-active': {
visibility: 'visible',
transform: 'translate3d(0,0,0)',
transition: 'all 0.4s'
},
'notification-leave-notification-leave-active': {
visibility: 'hidden',
transform: 'translate3d(100%,0,0)',
transition: 'all 0.4s'
}
}
return (
<ul style={style}>
<ReactCSSTransitionGroup
style={tstyle}
transitionName='notification'
transitionEnterTimeout={400}
transitionLeaveTimeout={400}>
{this.props.notifications.map((notification, index) => {
return <Notification
key={index}
type={notification.type}
message={notification.message}
timeout={10000}
remove={this.props.actions.removeNotification} />
})}
</ReactCSSTransitionGroup>
</ul>
)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Notifications)
解决方法:
ReactCSSTransitionGroup与内联样式不兼容,因为它向DOM节点添加类名或从DOM节点中删除类名以触发转换.但是您可以使用ReactTransitionGroup来制作自己的组件,该组件可以执行ReactCSSTransitionGroup的相同操作,但是具有内联样式.
如果您不想自己开发,可以使用我前一段时间写的一个,并在npm上安装它:ReactInlineTransitionGroup.
与ReactCSSTransitionGroup相比,它具有一些优点,例如您可以编写CSS过渡而不必担心在组件中设置超时属性,并且可以传递回调以检查孩子何时进入或离开组.