2017-07-10 8 views
0

私のアプリケーションでは、コンポーネントを下にレンダリングするFlatListコンポーネントがあります(情報の小さなスニペットを含む行が効果的に表示されます)。フラット・リスト内の各コンポーネントは、ユーザーをより詳細な別の画面に移動させます。コンポーネントをonPressに追加する(リアクションナビゲーション)

私は、各コンポーネントをクリック可能にすることができ、クリック可能であることを示すアラート()を実行させることができますが、React-Navigationを使用して追加するのは難しいことがわかります。

FlatListページ:

/* @flow*/ 

import _ from 'lodash' 
import {getAllQuestions} from './questionRepository' 
import ProfilePicture from './components/ProfilePicture' 
import QuestionerAndQuestion from './questionRow/QuestionerAndQuestion' 
import Separator from './components/Separator' 
import {Button, FlatList} from 'react-native' 
import React, {Component} from 'react' 

export default class QuestionList extends Component { 
    static navigationOptions = ({navigation}) => ({ 
     title: 'AIBU', 
     headerRight: (
      <Button 
       onPress={_.debounce(() => { 
        navigation.navigate('Add') 
       }, 500)} 
       title="Add" 
      /> 
     ), 
     headerStyle: { 
      backgroundColor: 'rgb(245, 245, 245)', 
     }, 
     headerLeft: <ProfilePicture size={'small'} />, 
    }) 

    state = {questions: []} 

    componentDidMount() { 
     this.getData() 
    } 

    async getData() { 
     const questions = await getAllQuestions() 

     this.setState({questions}) 
    } 

    render() { 
     return (
      <FlatList 
       ItemSeparatorComponent={Separator} 
       data={this.state.questions} 
       renderItem={({item}) => <QuestionerAndQuestion item={item} />} 
      /> 
     ) 
    } 
} 

QuestionerAndQuestionコンポーネント:

/* @flow */ 

import ProfilePicture from '../components/ProfilePicture' 
import React from 'react' 
import TextContainer from './TextContainer' 
import {StyleSheet, TouchableWithoutFeedback, View} from 'react-native' 

const styles = StyleSheet.create({ 
    row: { 
     backgroundColor: 'rgb(255, 255, 255)', 
     height: 125, 
     flex: 1, 
     flexDirection: 'row', 
    }, 
    profilePic: { 
     flex: 1, 
     justifyContent: 'center', 
     alignItems: 'center', 
     flexDirection: 'row', 
    }, 
    textBody: { 
     flex: 6, 
    }, 
}) 

const QuestionerAndQuestion = ({item}: {item: Object}) => 
    <TouchableWithoutFeedback onPress={() => navigate would go here?}> 
     <View style={styles.row}> 
      <View style={styles.profilePic}> 
       <ProfilePicture /> 
      </View> 
      <View style={styles.textBody}> 
       <TextContainer item={item} /> 
      </View> 
     </View> 
    </TouchableWithoutFeedback> 

export default QuestionerAndQuestion 
+0

以内

QuestionList以内

render() { const navigation = this.props.navigation return ( <FlatList ItemSeparatorComponent={Separator} data={this.state.questions} renderItem={({item}) => <QuestionerAndQuestion item={item} onPress={() => navigation.navigate('Question')} />} /> ) } 

あなたは "すべての情報を警告する" とはどういう意味ですか?私はあなたが何かをしたいと思っていて、次にナビゲートしていますか?あなたが実行しているエラーが何であるかを知るのは少し難しいです。 –

+0

@MichaelCheng明らかに申し訳ありませんが、行がクリック可能であることを示す警告()を行うことができます(今編集された質問)。しかし、この画面から別の画面にナビゲートする方法がわかりません。 QuestionListクラスで見ることができるように画面にナビゲートするが、QuestionerAndQuestionコンポーネントに追加する方法がわからない – rawr105

答えて

0

そこで私は、あなたがそこにnavigationオブジェクトを持って、あなたはQuestionListコンポーネントにナビゲートすると仮定します。あなたは何をしたいのですかnavigationを小道具QuestionerAndQuestionに渡してからonPressからアクセスできます。このような

何か:

<QuestionerAndQuestion item={item} navigation={this.props.navigation} />

ホープ、これはこれは私の問題を解決し

+0

'QuestionList'から' QuestionerAndQuestion'に小道具を送る方法を書くことができます – gaback

+0

これは表示されません作業するには、未定義のオブジェクトが渡されているように見える – rawr105

+0

これを動作させるには管理してください。 - コンポーネントQuestionerAndQuestionはプレゼンテーション用ですが、QuestionListは画面でナビゲーションを制御します。私の答えを見る – rawr105

0

に役立ちます。 QuestionerAndQuestion

const QuestionerAndQuestion = ({item, onPress}: {item: Object, onPress: Object}) => 
    <TouchableWithoutFeedback onPress={onPress}> 
関連する問題