2017-09-17 34 views
0

我正在嘗試創建文章列表。目前,我有第一個視圖顯示列表視圖中的所有文章。當我們點擊一​​篇文章時,我們必須重新指向顯示本文細節的其他視圖。但我不知道如何獲得文章的細節。轉到使用React-Native的文章列表的詳細視圖

'use strict'; 

import React, { Component } from 'react'; 
import { AppRegistry, StyleSheet, Text, View, ListView, TouchableHighlight, 
ActivityIndicator, Image } from 'react-native'; 
import { StackNavigator } from 'react-navigation'; 
import Article from './article'; 

export default class Home extends Component { 

    constructor(props) { 
    super(props); 
    this.state = { 
    isLoading: true 
    } 
} 

static navigationOptions = { 
    title: 'Articles' 
} 

componentDidMount() { 
    return fetch('http://www.femininbio.com/json/liste-articles') 
    .then((response) => response.json()) 
    .then((responseJson) => { 
    let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); 
    this.setState({ 
     isLoading: false, 
     dataSource: ds.cloneWithRows(responseJson), 
    }); 
    }) 
    .catch((error) => { 
    console.error(error); 
    }); 
} 

renderRow(rowData) { 
    return (
    <TouchableHighlight id={rowData.id} onPress={() => this.props.navigation.navigate('Article')} underlayColor='#F5FCFF' style={styles.item}> 
    <View style={styles.itemView}> 
     <Image style={styles.image} source={{uri: rowData.i.u}}/> 
     <View style={styles.blockText}> 
     <Text style={styles.titleArticle}>{rowData.t}</Text> 
     <Text style={styles.rubrique}>Rubrique : {rowData.r.n}</Text> 
     </View> 
    </View> 
    </TouchableHighlight> 
); 
} 

    render() { 
    if (this.state.isLoading) { 
     return (
     <View style={styles.loading}> 
      <ActivityIndicator/> 
     </View> 
    ); 
    } 

    return (
     <View style={styles.container}> 
     <ListView 
      style={styles.list} 
      dataSource={this.state.dataSource} 
      renderRow={this.renderRow.bind(this)} 
     /> 
     </View> 
    ); 
    } 
} 

所以我共進午餐stacknavigator的另一種觀點,我認爲我必須使用道具來獲得物品的ID和過濾數據,但對我來說有點模糊。

回答

0

在導航到新屏幕時,您可以將參數傳遞到新屏幕。使用您在新屏幕中獲得的參數,您可以進行新的抓取,或者僅使用您傳遞的信息在屏幕上顯示數據。您可以在react-navigation docs

例閱讀更多關於這個話題

<TouchableHighlight 
    id={rowData.id} 
    onPress={() => this.props.navigation.navigate('Article', { article: rowData})}> 
    // ... 
</TouchableHighlight> 

// And in your Article Screen 
console.log(this.props.navigation.state.params.article) // will print article object/data 
+0

好的,謝謝,我的文章屏幕,我一定要得到與包含有關文章的詳細信息的另一條鏈路的結果。所以我需要再次使用獲取才能獲得一個結果。可以爲一個結果創建一個提取? – Guillaume

+0

@Guillaume與你的API真的很相關 – bennygenel

相關問題