2017-06-19 13 views
1

詳細を表示するためにリストからいくつかの行データを次の画面に渡そうとしていますが、それを達成できないようです。 これは、のようなナビゲートする際、私は小道具を渡す方法です:React Native:1つの画面から別の画面にナビゲートする方法

_renderRow(row,sectionId, rowId, highlightRow) { 
 
     var self = this; 
 
     let navigate=this.props.navigation; 
 
       return (
 
      <TouchableOpacity onPress={() => navigate('ChatList',{row})}> 
 
      
 
........//ignored code

そして、他の画面ChatList.js上:また

import React, { Component } from 'react'; 
 
import { 
 
    StyleSheet, 
 
    Text, 
 
    View, 
 
    TouchableOpacity, 
 
    TextInput, 
 
    Image 
 
} from 'react-native'; 
 

 
import { StackNavigator } from 'react-navigation'; 
 

 

 
const ChatList =() => { 
 

 
    return ( 
 
    <View> 
 
    </View> 
 
); 
 
} 
 

 
ChatList.navigationOptions = { 
 
//trying to set the title from the data sent around here 
 
    title: 'ChatList Title', 
 
    headerStyle: { 
 
      backgroundColor: '#2196F3', 
 
     }, 
 
    headerTitleStyle: { 
 
     color: 'white', 
 
    }, 
 
    headerBackTitleStyle: { 
 
     color: 'white', 
 
    }, 
 
    headerTintColor: 'white', 
 
}; 
 

 

 
export default ChatList

注意します、私は0とは違ってstacknavigationの実装が異なっていますここでは、ここで私の全体の実装https://gist.github.com/SteveKamau72/f04b0a3dca03a87d604fe73767941bf2

.Checkoutは_renderRowが存在するから、完全なクラスです:

ChatGroup.js 

/** ChatGroup.js**/ 
 
//This code is component for file App.js to display group of chats 
 
    
 
import React, { Component } from 'react'; 
 
import { 
 
    StyleSheet, 
 
    ListView, 
 
    Text, 
 
    View, 
 
    Image, 
 
    TouchableOpacity 
 
} from 'react-native'; 
 
    
 
const data = [ 
 
    { 
 
     name: "Kasarini", 
 
     last_chat: { 
 
      updated_at:"22:13", 
 
      updated_by: "Steve Kamau", 
 
      chat_message: "Lorem Ipsum is pretty awesome if you know it" 
 
     }, 
 
     thumbnail: "https://randomuser.me/api/portraits/thumb/men/83.jpg" 
 
    
 
    }, 
 
    { 
 
     name: "Kabete", 
 
     last_chat: { 
 
      updated_at:"20:34", 
 
      updated_by: "Tim Mwirabua", 
 
      chat_message: "Lorem Ipsum is pretty awesome if you know it" 
 
     }, 
 
     thumbnail: "https://randomuser.me/api/portraits/thumb/men/83.jpg" 
 
    
 
    }, 
 
    { 
 
     name: "Kiambuu", 
 
     last_chat: { 
 
      updated_at:"19:22", 
 
      updated_by: "Maureen Chubi", 
 
      chat_message: "Lorem Ipsum is pretty awesome if you know it" 
 
     }, 
 
     thumbnail: "https://randomuser.me/api/portraits/thumb/men/83.jpg" 
 
    
 
    }, 
 
    { 
 
     name: "UnderPass", 
 
     last_chat: { 
 
      updated_at:"17:46", 
 
      updated_by: "Faith Chela", 
 
      chat_message: "Lorem Ipsum is pretty awesome if you know it" 
 
     }, 
 
     thumbnail: "https://randomuser.me/api/portraits/thumb/men/83.jpg" 
 
    
 
    }, 
 
] 
 
    
 
export default class UserListView extends Component { 
 
    constructor() { 
 
     super(); 
 
     const ds = new ListView.DataSource({rowHasChanged: this._rowHasChanged}); 
 
    
 
     this.state = { 
 
      dataSource: ds.cloneWithRows(data) 
 
     } 
 
    } 
 
    render() { 
 
     return ( 
 
      <ListView 
 
       dataSource={this.state.dataSource} 
 
       renderRow={this._renderRow.bind(this)} 
 
       enableEmptySections={true} /> 
 
     ) 
 
    } 
 
    
 
    _renderRow(row,sectionId, rowId, highlightRow) { 
 
     var self = this; 
 
     return ( 
 
      <TouchableOpacity activeOpacity={0.9} onPress={() => navigate('ChatList',{ user: 'Lucy' })}> 
 
       <View style={styles.container}> 
 
        <Image 
 
          style={styles.groupChatThumbnail} 
 
          source={{uri: row.thumbnail}}/> 
 
        <View> 
 
         <View style={{flexDirection:'row', justifyContent:'space-between', width:280}}> 
 
           <Text style={styles.groupNameText}>{row.name} </Text> 
 
           <Text style={styles.groupUpdatedAtText}>{row.last_chat.updated_at}</Text> 
 
    
 
         </View> 
 
         <View style={{ flexDirection:'row', alignItems:'center', marginTop: 5}}> 
 
           <Text style={styles.groupUpdatedByText}>{row.last_chat.updated_by} : </Text>  
 
           <View style={{flex: 1}}> 
 
             <Text ellipsizeMode='tail' numberOfLines={1}style={styles.groupChatMessageText}>{row.last_chat.chat_message} </Text> 
 
           </View> 
 
         </View> 
 
        </View> 
 
         
 
       
 
       </View> 
 
      </TouchableOpacity> 
 
     ) 
 
    } 
 
    
 
    _rowHasChanged(r1, r2) { 
 
     return r1 !== r2 
 
    } 
 
    
 
    
 
     highlightRow() { 
 
    alert('Hi!'); 
 
    } 
 
    
 
} 
 
    
 
const styles = StyleSheet.create({ 
 
    container:{ 
 
     alignItems:'center', 
 
     padding:10, 
 
     flexDirection:'row', 
 
     borderBottomWidth:1, 
 
     borderColor:'#f7f7f7', 
 
     backgroundColor: '#fff' 
 
     }, 
 
     groupChatContainer:{ 
 
     display: 'flex', 
 
     flexDirection: 'row', 
 
     
 
     }, 
 
     groupNameText:{ 
 
     marginLeft:15, 
 
     fontWeight:'600', 
 
     marginTop: -8, 
 
     color: '#000' 
 
     }, 
 
     groupUpdatedAtText :{ 
 
     color:'#333', fontSize:10, marginTop: -5 
 
     }, 
 
     groupChatThumbnail:{ 
 
     borderRadius: 30, 
 
     width: 50, 
 
     height: 50 , 
 
     alignItems:'center' 
 
     }, 
 
     groupUpdatedByText:{ 
 
     fontWeight:'400', color:'#333', 
 
     marginLeft:15, marginRight:5 
 
     }, 
 
    
 
});

+0

_renderRowが属するクラスのコンストラクタを表示してください。 – Ismailp

+0

兄弟コンポーネント間でデータを渡そうとしていると言うと、合理的な解釈になりますか? – SoZettaSho

+0

@Ismailp私の更新されたコードを見てください。 –

答えて

7

2番目の画面上のナビゲーション小道具にアクセスするには2つの方法があります。

  1. 内部のようなあなたはなど、レンダリングのような任意のメソッド内でアクセスする場合

    navigationOptions = ({navigation}) => ({title:`${navigation.state.params.name}`}); 
    
  2. は、これが書かれた可能性が

    {user}= this.props.navigation.state.params 
    

ChatList.navigationOptionsは、この内部

ChatList.navigationOptions= ({navigation}) => ({// props access here }); 

const ChatList =()の通りであります書くことができますthis.props.navigation.state.params

+0

仲間ありがとう、私の問題を解決しました –

+0

あなたは歓迎です – Rahul

関連する問題