2017-09-28 118 views
1

我是新來的反應原生如何從一個屏幕數據發送到使用道具的Android另一個畫面不是IOS我的代碼如下如何將數據從一個屏幕發送到另一個屏幕?

Home.js

class Home extends React.Component { 

    constructor(props) { 
    super(props); 
    this.state = { 
     qwerty:{ 
     data:[], 
     }, 
    }; 
    } 

    goPressed(navigate){ 
    navigate("Product"); 
    } 

    render() { 
     const { navigate } = this.props.navigation; 

     contents = this.state.qwerty.data.map((item) => { 
     return (
      <View key={item.p1.id}> 
      <View> 
       <Text>{item.p1.content}</Text> 
      </View> 
      <View> 

      <TouchableHighlight onPress={() => this.goPressed(navigate)}> 
       <Text> 
       Go 
       </Text> 
      </TouchableHighlight> 

      </View> 
      </View> 
     ); 
     }); 

     return (
     <ScrollView style={styles.container}> 
      {contents} 
     </ScrollView> 
    ); 
    } 
    } 

    export default Home; 

這是我的家。 JS,我想要傳遞數據,例如{item.p1.content}到另一個屏幕,即product.js,那麼我該怎麼做呢我應該做些什麼修改?

Product.js

 export default class Products extends Component { 
    static navigationOptions = { 
    title: "Products", 
    }; 

    render() { 
    return (
     <View style={{ flex: 1 }}> 
     <Text>{item.p1.content}</Text> 
     </View> 
    ); 
    } 
} 

回答

1

一種方法是簡單地傳遞你存儲在日期「QWERTY '作爲下一個場景的道具。

在Home.js你可以修改你goPressed方法是這樣的......

goPressed(navigate){ 
    navigate("Product", {passedData: this.state.qwerty.item.p1.content}); 
    } 

然後在Product.js您需要修改代碼以

render() { 
    return (
     <View style={{ flex: 1 }}> 
     <Text>{this.props.passedData}</Text> 
     </View> 
    ); 
    } 
+0

你好..使用這種方法,它正在導航,但數據不會傳遞到'產品'並顯示'未定義'時,在控制檯上查詢。是否需要在'Product.js'中'輸入'某些東西或'set state'。 –

+0

當您將Home.js更改爲: navigate(「Product」,{passedData:this.state.qwerty}); – Teedub

相關問題