ReactJs从Props而不是State获取数据

时间:2017-06-13 10:34:47

标签: reactjs properties state

我如何从道具中获取我的数据,而不是从新的状态获取反应,并且我真的不知道如何处理它是我的垃圾箱:my app

任何建议或例子?

1 个答案:

答案 0 :(得分:1)

我已更改您应用中的代码请在此处查看Demo

import React from 'react'
import {render} from 'react-dom'
import { CSSTransitionGroup } from 'react-transition-group'



class SendData extends React.Component{
  constructor(props) {
    super(props);
     this.state = {
      images: [
        'http://via.placeholder.com/350x150',
        'http://via.placeholder.com/350x151'
      ],
      currentImage: 0
    };
    this.fadeImage=this.fadeImage.bind(this);
  }
  fadeImage(e) {
    e.preventDefault();
    this.setState({currentImage: (this.state.currentImage + 1) % this.state.images.length})
  }
  render()
  {

    return(
      <FadeImage images={this.state.images} currentImage={this.state.currentImage} fadeImage={this.fadeImage}/>
     )
  }
}
class FadeImage extends React.Component {

  constructor(props) {
    super(props);
  }



  render() {
    return (
            <div className="image">
      <CSSTransitionGroup
        transitionName="example"
        transitionEnterTimeout={300}
        transitionLeaveTimeout={300}
        >
          <section>
            <button className="button" onClick={this.props.fadeImage.bind(this)}>Click!</button>
            <img src={this.props.images[this.props.currentImage]}/></section>
        </CSSTransitionGroup>
        </div>
      );
    }
  }


render(<SendData />, document.querySelector('#app'))