React Native mobile development practices-3-implement data transfer between pages, react-3-

Source: Internet
Author: User

React Native mobile development practices-3-implement data transfer between pages, react-3-

React Native uses props to transmit and communicate data between pages. In React Native, there are two ways to store and transmit data: props (attribute) and state (state), where:

  • Props is usually specified in the parent component, and once specified, it will not change in the lifecycle of the specified component.
  • State is usually used to store the data to be changed. When the state data is updated, React Native refreshes the interface.

After learning about the difference between props and state, you should know that to pass the data on the home page to the next page, you need to use props. Therefore, modify the home. js Code as follows:

 

Export default class home extends React. component {// The unmodified code _ renderRow = (rowData, sectionID, rowID) => {return (<TouchableHighlight onPress = {() is omitted here {() ==>{ const {navigator} = this. props; // obtain navigatorif (navigator) {navigator from props. push ({name: 'detail', component: detail, params: {productTitle: rowData. title // pass props through params}); }}>// the unmodified code is omitted here </TouchableHighlight> );}}

  

In the home. in js, the parameter params added for the push method of Navigator is passed to the next page as a props. this. props. productTitle to obtain the data transmitted on the home page. Modify the detail. js Code as follows:

Export default class detail extends React. component {render () {return (<View style = {styles. container }> <TouchableOpacity onPress = {this. _ pressBackButton. bind (this) }> <Text style = {styles. back }> return </Text> </TouchableOpacity> <Text style = {styles. text }>{ this. props. productTitle} </Text> </View>);} // No code modified here}

  

Reload the application. When you click the product list again, the product name is displayed on the details page, as shown in Figure 3.31.

Figure 3.31 product name displayed on the details page

 

In this way, a complete page Jump and data transfer between pages are achieved.

Learn With Me, React Native mobile development practices

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.