【问题标题】:React Native navigation for child components in flat list平面列表中子组件的 React Native 导航
【发布时间】:2017-11-04 14:05:35
【问题描述】:

我是 React Native(和 Javscript)的新手,我正在尝试理解 React Native 导航。

我有一个homeScreen,在这个屏幕中有一个CardFlatList,它有几个CardComponents。如果我单击 CardComponent,将打开一个详细信息页面并表示 cardComponent 的信息。

首先,我尝试使用 homeScreen 中的按钮进行导航,并且成功了。但我不知道如何从主屏幕传递导航器 -> CardFlatList -> CardComponent (onPress)。

我的卡片组件

export default class CardComponent extends Component {
constructor(props) {
    super(props)
}

render() {
    return (
        <View style={styles.container}>
            <Card
                featuredTitle={this.props.eventname}
                featuredSubtitle={this.props.eventveranstalter}
                image={{uri: this.props.eventimage}}
            >
                <View style={styles.chipContainer}>
                    <ChipsComponent text={this.props.eventtag1} style={{ marginBottom: 10 }} />
                    <ChipsComponent text={this.props.eventtag2} style={{ marginBottom: 10 }} />
                    <ChipsComponent text={this.props.eventtag3} style={{ marginBottom: 10 }} />
                </View>
                <View style={styles.cardaction}>
                    <Text style={styles.cardfooter}>
                        {this.props.eventuhr}
                    </Text>
                    <Text style={styles.cardfooter}>
                          {this.props.eventort}
                    </Text>
                    <Button
                        backgroundColor='#5ac8fa'
                        fontFamily='Arial'
                        fontWeight='bold'
                        buttonStyle={styles.buttonfooter}
                        title='Teilnehmen' />
                </View>
            </Card>
        </View>
    )
}

我的主屏幕

export default class homeScreen extends Component {
  static navigationOptions = {
    drawerLabel: 'Home',
    headerMode: 'none',

  };

  constructor(props) {
    super(props)
  }


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

      <View style={styles.containerAll}>
        <Navigationbar onPressBar={()=>this.props.navigation.navigate('DrawerOpen')} />


        <View style={styles.container}>
          <CardFlatList navigation={this.props.navigation} />
        </View>

      </View>
    );
  }

}

我的路由器.js

export const DrawNavigation = DrawerNavigator({
  Home: {
    screen: homeScreen
  },
  Notifications: {
    screen: test,
  },
});



export const Root = StackNavigator({

  DrawNavigation: {
    screen: DrawNavigation,
    navigationOptions: {
      header: false,

    },

  },
  First: { screen: homeScreen },
  Second: { screen: eventDetailScreen },





});

import React, { Component } from "react";
import { View, Text, FlatList, ActivityIndicator,Image } from "react-native";
import { List, ListItem, SearchBar } from "react-native-elements";
import CardComponent from './CardComponent';

function getCardComponentArray() {
    var texts = ['1', '2', '3', '4', '5'];
    var cardComponents = texts.map((text) => <CardComponent title={text} />);
    return cardComponents;
}

function getData() {
    var texts = ['1', '2', '3', '4', '5'];
    return texts
}


class CardFlatList extends Component {
    constructor(props) {
        super(props);

        this.state = {
            loading: false,
            data: [],
            page: 1,
            seed: 1,
            error: null,
            refreshing: false
        };
    }

    componentDidMount() {
        this.makeRemoteRequest();
    }

    makeRemoteRequest = () => {
        this.setState({
            data: getData(),
            error: null,
            loading: false,
            refreshing: false
        });
    };

    handleRefresh = () => {
        this.setState(
            {
                page: 1,
                seed: this.state.seed + 1,
                refreshing: true
            },
            () => {
                this.makeRemoteRequest();
            }
        );
    };

    handleLoadMore = () => {
        this.setState(
            {
                page: this.state.page + 1
            },
            () => {
                this.makeRemoteRequest();
            }
        );
    };

    renderSeparator = () => {
        return (
            <View
                style={{
                    height: 2,         
                }}
            />
        );
    };

    renderHeader = () => {
        return <SearchBar placeholder="Type Here..." lightTheme round />;
    };

    renderFooter = () => {
        if (!this.state.loading) return null;

        return (
            <View
                style={{
                    paddingVertical: 20,
                    borderTopWidth: 1,

                }}
            >
                <ActivityIndicator animating size="large" />
            </View>
        );
    };

    render() {
        return (
        //    <List>
                <FlatList
                    data={this.state.data}
                    renderItem={({ item }) => (


                        <CardComponent 
                        eventname={'Halloween'}
                        eventveranstalter={'park'}
                        eventuhr={'17:00'}
                        eventort={'berlin'}
                        eventimage={'http://whatstonightapp.com/wp-content/uploads/2016/12/background_light-1.jpg'}
                        eventtag1={'party'}
                        eventtag2={'music'}
                        eventtag3={'dance'}
                        >

                        </CardComponent>

                    )}
                    keyExtractor={item => item.email}
                    ItemSeparatorComponent={this.renderSeparator}
             //     ListHeaderComponent={this.renderHeader}
                    ListFooterComponent={this.renderFooter}
                    onRefresh={this.handleRefresh}
                    refreshing={this.state.refreshing}
                    onEndReached={this.handleLoadMore}
                    onEndReachedThreshold={50}
                />
      //    </List>
        );
    }
}

export default CardFlatList;

【问题讨论】:

  • 你也可以在这里添加 CardFlatList 吗?
  • 是的,我添加了它。对不起,我忘记了。

标签: javascript reactjs react-native


【解决方案1】:

你可以这样做。

    render() {
        return (
        //    <List>
                <FlatList
                    data={this.state.data}
                    renderItem={({ item }) => (


                        <CardComponent
                        navigation={this.props.navigation}
                        eventname={'Halloween'}
                        eventveranstalter={'park'}
                        eventuhr={'17:00'}
                        eventort={'berlin'}
                        eventimage={'http://whatstonightapp.com/wp-content/uploads/2016/12/background_light-1.jpg'}
                        eventtag1={'party'}
                        eventtag2={'music'}
                        eventtag3={'dance'}
                        >

                        </CardComponent>

                    )}

【讨论】:

  • 感谢您的回答。我现在如何使用导航器?我将卡片中的视图更改为 TouchableOpacity,并尝试使用 onPress 但我不工作
  • 在您现在拥有this.props.navigation 的每张卡片中,您应该使用它。如果它不起作用,请发布更新的代码。
猜你喜欢
  • 2019-07-02
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多