rnn
rnn

Reputation: 2553

How to pass data between Components?

I want to pass data to another component state. But I dont want to open that page I mean Idont want to use navigate.

I use componenwillmount on that page but when data change, componenwillmount not work again.

I pass like this.

<NewPage data={this.state.collection} />

this is page1 :

return (

    <View style= {styles.firstView}> 
         <View style={{flex: 1.5}}>
             <Header color1 = {Blue.length}  color2 = {Yellow.length} navigation={this.props.navigation}  setData= {this.handleSetData}/>
         </View >

             <View style={{flex: 0.5, backgroundColor:'#f2f2f2'}}>
                 <Bar />
            </View>

        <View style={{flex: 9}}>
            {/* <ScrollView>
             {this.renderall()}
            </ScrollView> */}

            <NewPage data= {this.state.collection}/>


        </View>

        <View style={styles.footerStyle}>
          <Footer />
        </View>  

    </View>

this is NewPage:

export default class NewPage extends Component {

  constructor(props) {
    super(props);
    this.state= {

    fromPage1: [],
    }

}

As I said everytime I want to change state which is "frompage1"

Upvotes: 0

Views: 63

Answers (1)

Subhendu Kundu
Subhendu Kundu

Reputation: 3856

I am quite not sure what you are asking. If I got you right, I think you are looking for something like this

export default class NewPage extends Component {

  constructor(props) {
    super(props);
const {data} = props;
console.log(data); //just to make sure you are getting right set of data
    this.state= {

    fromPage1: data
    }

}

// if your props are updating try
componentWillReceiveProps(nextProps) { this.setState(fromPage1: nextProps.data)}

Let me know if you wanted something else.

Upvotes: 1

Related Questions