首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用React和React过渡组在页面之间淡入淡出

使用React和React过渡组在页面之间淡入淡出
EN

Stack Overflow用户
提问于 2018-04-13 16:16:02
回答 1查看 814关注 0票数 1

我正在尝试使用React Starter Kit在React中淡入淡出和离开页面之间。

受post Applying React.js CSS Transitions on initial render的启发,我为每个页面加载的根组件做了以下操作:

代码语言:javascript
复制
import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import s from './About.less';

class About extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            mounted: false,
        };
    }
    componentDidMount = () => {
        this.setState({
            mounted: true,
        });
    };
    render() {
        const child = this.state.mounted ? <h1>Hello world</h1> : null;

        return (
            <ReactCSSTransitionGroup
                transitionName="example"
                transitionAppear
                transitionEnterTimeout={500}
                transitionLeaveTimeout={300}
            >
                {child}
            </ReactCSSTransitionGroup>
        );
    }
}

export default withStyles(s)(About);

在css中,我有:

代码语言:javascript
复制
.example-appear {
  opacity: 0.01;
  transition: opacity 0.5s ease-in;
}

.example-appear.example-appear-active {
  opacity: 1;
}

安装组件时,将显示元素,但没有任何进入过渡。我是不是做错了什么?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-04-14 00:54:55

在ComponentDidMount()中/之后,显示过渡将不起作用。

如果想要查看ComponentDidMount()之后的转换,请使用Enter/Leave transition。

在您的例子中,如果您将代码放在componentWillMount()中,而不是componentDidMount(),它将很好地工作。我已经修改了你的代码。希望这能有所帮助。

代码语言:javascript
复制
class About extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        mounted: false
    };
}
componentWillMount ()  {
    this.setState({
        mounted: true
    });
};
render() {
    const child = this.state.mounted ? <h1>Hello world</h1> : null;

    return (
        <ReactCSSTransitionGroup
            transitionName="example"
            transitionAppear

        >
            {child}
        </ReactCSSTransitionGroup>
    );
}

}

参考React Page Transition

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49812309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档