react-transition-group CSSTransition,条件为 null
react-transition-group CSSTransition, conditional with null
正在尝试实现简单的淡入淡出 in/out 过渡。
我试过将 <CSSTransition>
移动到不同的区域,但无济于事。我在另一个映射子组件的组件中成功地使用了它,但看不出为什么它在这种情况下不起作用,因为我将它与子组件一起渲染,如果子组件完全返回的话。
子组件
const Error = (props) => {
return (
<CSSTransition timeout={400} classNames={errorTransition}>
<span> {props.errorString} </span>
</CSSTransition>
)
}
父组件
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { CSSTransition } from 'react-transition-group';
import type { InfoState } from './state';
import { closeError } from './actions';
const mapStateToProps = (state: {info: InfoState}) => ({
info: state.info.data.info,
infoError: state.info.infoError,
});
const mapDispatchToProps = dispatch => ({
closeError: () => dispatch(closeError()),
});
class Parent extends Component<Props, State> {
state = { info: this.props.info };
handleChange = (e) => {
this.setState({ info: e.target.value });
this.props.closeError();
}
render() {
if (this.props.info === null) {
return (
<div className="info-wrapper">
<input
type="text"
value={this.state.info ? this.state.info : '' }
onChange={this.handleChange}
/>
</div>
<div className="info-error">
{ this.props.infoError !== ''
? <Error
key={this.state.info}
errorString={this.props.infoError}
/>
: null
}
</div>
)
}
return ( <div> things </div> )
}
}
CSS
.errorTransition-enter {
opacity: 0.01;
}
.errorTransition-enter-active {
opacity: 1;
transition: all 400ms ease-out;
}
.errorTransition-exit {
opacity: 1;
}
.errorTransition-exit-active {
opacity: 0.01;
transition: all 400ms ease-out;
}
我在有条件地删除用 <CSSTransition>
包裹的元素时遇到了类似的问题。为了解决这个问题,我用 <TransitionGroup>
元素包裹了 <CSSTransition>
元素,并使用了它的 childFactory 属性。 childFactory
道具可以像这样使用:
<TransitionGroup
childFactory={child => React.cloneElement(child)}
>
<CSSTransition timeout={400} classNames={errorTransition}>
<span> {props.errorString} </span>
</CSSTransition>
</TransitionGroup>
正在尝试实现简单的淡入淡出 in/out 过渡。
我试过将 <CSSTransition>
移动到不同的区域,但无济于事。我在另一个映射子组件的组件中成功地使用了它,但看不出为什么它在这种情况下不起作用,因为我将它与子组件一起渲染,如果子组件完全返回的话。
子组件
const Error = (props) => {
return (
<CSSTransition timeout={400} classNames={errorTransition}>
<span> {props.errorString} </span>
</CSSTransition>
)
}
父组件
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { CSSTransition } from 'react-transition-group';
import type { InfoState } from './state';
import { closeError } from './actions';
const mapStateToProps = (state: {info: InfoState}) => ({
info: state.info.data.info,
infoError: state.info.infoError,
});
const mapDispatchToProps = dispatch => ({
closeError: () => dispatch(closeError()),
});
class Parent extends Component<Props, State> {
state = { info: this.props.info };
handleChange = (e) => {
this.setState({ info: e.target.value });
this.props.closeError();
}
render() {
if (this.props.info === null) {
return (
<div className="info-wrapper">
<input
type="text"
value={this.state.info ? this.state.info : '' }
onChange={this.handleChange}
/>
</div>
<div className="info-error">
{ this.props.infoError !== ''
? <Error
key={this.state.info}
errorString={this.props.infoError}
/>
: null
}
</div>
)
}
return ( <div> things </div> )
}
}
CSS
.errorTransition-enter {
opacity: 0.01;
}
.errorTransition-enter-active {
opacity: 1;
transition: all 400ms ease-out;
}
.errorTransition-exit {
opacity: 1;
}
.errorTransition-exit-active {
opacity: 0.01;
transition: all 400ms ease-out;
}
我在有条件地删除用 <CSSTransition>
包裹的元素时遇到了类似的问题。为了解决这个问题,我用 <TransitionGroup>
元素包裹了 <CSSTransition>
元素,并使用了它的 childFactory 属性。 childFactory
道具可以像这样使用:
<TransitionGroup
childFactory={child => React.cloneElement(child)}
>
<CSSTransition timeout={400} classNames={errorTransition}>
<span> {props.errorString} </span>
</CSSTransition>
</TransitionGroup>