【问题标题】:How to navigate to next screen with data in react native如何使用本机反应中的数据导航到下一个屏幕
【发布时间】:2021-08-17 05:15:16
【问题描述】:

我是 React Native 应用程序开发的新手。我的问题是我也尝试使用数据(字符串和图像)导航到第一个屏幕到第二个屏幕。但会尝试导航错误。错误是未定义导航。请解决我的问题并编辑我的代码。我已附上我的代码供您参考。

    import React from 'react';
    import { StyleSheet, Text, View, FlatList, Image, TouchableOpacity } from 'react-native';
function Item({ item }) {
  return (
    <View style={styles.listItem}>
      <Image source={{uri:item.photo}}  style={{width:60, height:60,borderRadius:30}} />
      <View style={{alignItems:"center",flex:1}}>
        <Text style={{fontWeight:"bold"}}>{item.name}</Text>
        <Text>{item.position}</Text>
      </View>
      <TouchableOpacity 
      onPress={() => getItem(item)}
      style={{height:50,width:50, justifyContent:"center",alignItems:"center"}}>
        <Text style={{color:"green"}}>Call</Text>
      </TouchableOpacity>
    </View>
  );
}
 const getItem = (item) => {
    // Function for click on an item
    alert('Id : ' + item.id + ' Title : ' + item.position);
  };
  
 const getpress=(item)=> {
    this.props.navigation.navigate('SecondPage', {
      itemId: item.name,
      title: item.position.rendered,
    });
  }


export default class FirstPage extends React.Component {
  state = {
    data:[
        {
            "name": "Miyah Myles",
            "email": "miyah.myles@gmail.com",
            "position": "Data Entry Clerk",
            "photo": "https:\/\/images.unsplash.com\/photo-1494790108377-be9c29b29330?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=707b9c33066bf8808c934c8ab394dff6"
        },
        {
            "name": "June Cha",
            "email": "june.cha@gmail.com",
            "position": "Sales Manager",
            "photo": "https:\/\/randomuser.me\/api\/portraits\/women\/44.jpg"
        }
    ]
  }

  render(){
    //const { navigate } = this.props.navigation;
    return (
      <View style={styles.container}>
        <TouchableOpacity onPress={()=>navigate('SecondPage')}>
        <FlatList
          style={{flex:1}}
          data={this.state.data}
          renderItem={({ item }) => <Item item={item}/>}
          keyExtractor={item => item.email}
        />
        </TouchableOpacity>
      </View>
    );
  } 
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F7F7F7',
    marginTop:60
  },
  listItem:{
    margin:10,
    padding:10,
    backgroundColor:"#FFF",
    width:"80%",
    flex:1,
    alignSelf:"center",
    flexDirection:"row",
    borderRadius:5
  }
});

【问题讨论】:

    标签: javascript reactjs react-native onclick react-native-flatlist


    【解决方案1】:

    我假设您希望在使用参数按下 Flatlist 项目时导航到第二页。所以不要把完整的平面列表放在一个 Touchable 中,尽管只用 Touchable 包装了 Item 并将 onPress 作为 props 从父项传递给 Item。

    代码如下:

    import React from 'react';
    import {
      StyleSheet,
      Text,
      View,
      FlatList,
      Image,
      TouchableOpacity,
    } from 'react-native';
    
    function Item({item, onPress}) {
      return (
        <View style={styles.listItem}>
          <Image
            source={{uri: item.photo}}
            style={{width: 60, height: 60, borderRadius: 30}}
          />
          <View style={{alignItems: 'center', flex: 1}}>
            <Text style={{fontWeight: 'bold'}}>{item.name}</Text>
            <Text>{item.position}</Text>
          </View>
          <TouchableOpacity
            onPress={() => onPress(item)}
            style={{
              height: 50,
              width: 50,
              justifyContent: 'center',
              alignItems: 'center',
            }}>
            <Text style={{color: 'green'}}>Call</Text>
          </TouchableOpacity>
        </View>
      );
    }
    const getItem = item => {
      // Function for click on an item
      alert('Id : ' + item.id + ' Title : ' + item.position);
    };
    
    export default class FirstPage extends React.Component {
      state = {
        data: [
          {
            name: 'Miyah Myles',
            email: 'miyah.myles@gmail.com',
            position: 'Data Entry Clerk',
            photo:
              'https://images.unsplash.com/photo-1494790108377-be9c29b29330?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=707b9c33066bf8808c934c8ab394dff6',
          },
          {
            name: 'June Cha',
            email: 'june.cha@gmail.com',
            position: 'Sales Manager',
            photo: 'https://randomuser.me/api/portraits/women/44.jpg',
          },
        ],
      };
    
      getpress = item => {
        this.props.navigation.navigate('SecondPage', {
          itemId: item.name,
          title: item.position.rendered,
        });
      };
    
      render() {
        //const { navigate } = this.props.navigation;
        return (
          <View style={styles.container}>
            <FlatList
              style={{flex: 1}}
              data={this.state.data}
              renderItem={({item}) => <Item item={item} onPress={this.getpress} />}
              keyExtractor={item => item.email}
            />
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#F7F7F7',
        marginTop: 60,
      },
      listItem: {
        margin: 10,
        padding: 10,
        backgroundColor: '#FFF',
        width: '80%',
        flex: 1,
        alignSelf: 'center',
        flexDirection: 'row',
        borderRadius: 5,
      },
    });
    

    【讨论】:

    • 不工作,我有触摸没有反应
    【解决方案2】:

    在您的第一页:

    const getpress = (item) => {
            this.props.navigation.navigate('SecondPage', {
              itemId: item.name,
              title: item.position.rendered
            });
          }
    

    在您的第二页中:

    function SecondPage({ route, navigation }) {
          const { itemId, title } = route.params;
          return (
             //...
          );
        }
    

    【讨论】:

    • 如何实现显示下一个屏幕。
    • 尝试在根 App.js 中配置导航,如 this
    猜你喜欢
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2021-07-19
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多