2017-06-13 60 views
0

我怎么能代替从国家进出口新的反应得到的道具我的数据和真的不知道如何处理它,这里是我斌:my appReactJs获取数据从道具,而不是国家

任何建议或例子?

+0

国家是使用一个组件,而道具是用从一个组件传递到另一个价值,现在告诉我,为什么你需要的状态道具中的数据?或让我知道你想在你的示例应用程序中做什么? –

+0

我想从另一个组件再次收集道具的数据,所以我必须用无状态组件重新组织这个数据 – Alex

回答

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'))