最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React: How to animate the change of the component rendered? - Stack Overflow

programmeradmin4浏览0评论

I change the ponent that is rendered through a time interval.

I would like to be able to add an animation every time that change happens. What is the best way to go about it?

constructor (props) {
    super(props)
    this.state = { currentComponent: 1,
    numberOfComponents: 2}
}

ponentWillMount() {
    setInterval(() => {
       if(this.state.currentComponent === 2) {
           this.setState({currentComponent: 1})
       } else {
           this.setState({currentComponent: this.state.currentComponent + 1})
       }
    }, 5000)
}

render(){

    let currentComponent = null;

    if(this.state.currentComponent === 1) {
        currentComponent = <ComponentOne/>;

    } else {
        currentComponent = <ComponentTwo/>;
    }

    return(
        currentComponent
    )
}

EDIT:

Also when trying to use 'react-addons-css-transition-group' I get the following error:

I change the ponent that is rendered through a time interval.

I would like to be able to add an animation every time that change happens. What is the best way to go about it?

constructor (props) {
    super(props)
    this.state = { currentComponent: 1,
    numberOfComponents: 2}
}

ponentWillMount() {
    setInterval(() => {
       if(this.state.currentComponent === 2) {
           this.setState({currentComponent: 1})
       } else {
           this.setState({currentComponent: this.state.currentComponent + 1})
       }
    }, 5000)
}

render(){

    let currentComponent = null;

    if(this.state.currentComponent === 1) {
        currentComponent = <ComponentOne/>;

    } else {
        currentComponent = <ComponentTwo/>;
    }

    return(
        currentComponent
    )
}

EDIT:

Also when trying to use 'react-addons-css-transition-group' I get the following error:

Share Improve this question edited Jan 5, 2017 at 15:11 criver.to asked Jan 4, 2017 at 20:35 criver.tocriver.to 5403 gold badges9 silver badges20 bronze badges 1
  • The easiest in my opinion is using CSS3 transitions. You give your elements a class based on your state, which changes your element's styling. You can transition this styling using the CSS3 transitions (or CSS3 animations). – stinodes Commented Jan 4, 2017 at 23:07
Add a ment  | 

1 Answer 1

Reset to default 1

You can do with ReactCSSTransitionGroup like provided in this section

Your css:

.example-enter {
  opacity: 0.01;
}

.example-enter.example-enter-active {
  opacity: 1;
  transition: opacity 500ms ease-in;
}

.example-leave {
  opacity: 1;
}

.example-leave.example-leave-active {
  opacity: 0.01;
  transition: opacity 300ms ease-in;
}

Will look like this:

import ReactCSSTransitionGroup from 'react-addons-css-transition-group';



class MyComponent extends Component {
  constructor (props) {
    super(props)
    this.state = { currentComponent: 1,
    numberOfComponents: 2}
  }

  ponentWillMount() {
    setInterval(() => {
       if(this.state.currentComponent === 2) {
           this.setState({currentComponent: 1})
       } else {
           this.setState({currentComponent: this.state.currentComponent + 1})
       }
    }, 5000)
  }

  render(){

    let currentComponent = null;

    if(this.state.currentComponent === 1) {
        currentComponent = <ComponentOne/>;

    } else {
        currentComponent = <ComponentTwo/>;
    }

    return(
        <ReactCSSTransitionGroup
          transitionName="example"
           transitionEnterTimeout={500}
          transitionLeaveTimeout={300}>
          {currentComponent}
        </ReactCSSTransitionGroup>

    )
  }
}
发布评论

评论列表(0)

  1. 暂无评论