如何在react-native中传递state.params

问题描述 投票:0回答:2

我对反应原生有点陌生,我正在尝试使用反应原生地图做一些基本的应用程序。

我有一个登录屏幕,登录屏幕后我有一个用于不同条目的不同抽屉。在其中一个中,我调用 ManageBinScreen 并在 ManageBinScreen 中调用我也创建的 MapScreen 组件。

我创建了一个mapScreen作为类组件,并在该组件内调用MarkerDetail,还在MarkerDetail内调用markerEdit组件。但我收到一个错误:

组件异常未定义不是对象(评估'state.params')

我找不到解决此错误的方法,我知道它很长,但这是我的代码:

管理BinScreen:

import React from 'react';
import MapScreen from '../mapScreen'

export default class AManageBinScreen extends React.Component {
  render() {
    return (
      <MapScreen navigation={ this.props.navigation }/>
    );    
  }
}
module.exports = AManageBinScreen;

地图屏幕:

import { StyleSheet, TouchableHighlight, Text, View } from 'react-native';
import React from 'react';

import MapView from 'react-native-maps'; // remove PROVIDER_GOOGLE import if not using Google Maps



export default class mapScreen extends React.Component {

  constructor(props) {
    super(props);
    this.goToMarkerDetail = this.goToMarkerDetail.bind(this);
  }

  state = { reports: [] };

  componentDidMount() {
    fetch('https://my-json-server.typicode.com/UmutYvz/JSONHOLDER/report')
      .then(res => res.json())
      .then(data => {
        //console.log(data)
        this.setState({ reports: data.reports })
      })
      .catch(console.error)
  }

  goToMarkerDetail(id) {
    //console.log(this.state.reports)
    this.props.navigation.navigate('MarkerDetail', {
      id: id,
      adress: this.state.reports[id].adress,
      street: this.state.reports[id].street,
      district: this.state.reports[id].district,
      city: this.state.reports[id].city,
      country: this.state.reports[id].county,
      info: this.state.reports[id].info,
      lon: this.state.reports[id].lon,
      lat: this.state.reports[id].lat,
    })
  }

  render() {
    return (
      <MapView
        style={{ ...StyleSheet.absoluteFillObject }}
        initialRegion={{
          latitude: 41.249374,
          longitude: 32.682974,
          latitudeDelta: 0.015,
          longitudeDelta: 0.015
        }} >
        {this.state.reports.map((report) =>
          <MapView.Marker
            key={report.id}
            coordinate={{
              latitude: report.lat,
              longitude: report.lon
            }}
            title={report.info}
            description={report.street}

          >
            <MapView.Callout tooltip onPress={() => this.goToMarkerDetail(report.id)} >
              <TouchableHighlight style={styles.container}>
                <View>
                  <Text style={styles.upperText}>{report.info}</Text>
                  <Text style={styles.lowerText}>{report.city}, {report.district}, {report.street}, {report.adress}</Text>
                </View>
              </TouchableHighlight>
            </MapView.Callout>

          </MapView.Marker>
        )}
      </MapView>
    );
  }
}
module.exports = mapScreen;

标记详情

import React from 'react';
import { StyleSheet, View, Text, TouchableHighlight, SafeAreaView, ScrollView } from 'react-native';
import MapView from 'react-native-maps';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome'
import { faEdit, faTrashAlt } from '@fortawesome/free-solid-svg-icons'


const mapDetailComponent = (props) => {

  const { state } = props.navigation;
  const info = {
    markerData: {
      id: state.params.id,
      adress: state.params.adress,
      street: state.params.street,
      district: state.params.district,
      city: state.params.city,
      country: state.params.country,
      info: state.params.info,
      latitude: state.params.lat,
      longitude: state.params.lon,
    },
    mapData: {
      latitude: state.params.lat,
      longitude: state.params.lon,
      latitudeDelta: 0.015,
      longitudeDelta: 0.0121,
    },
  };

  return (
    <View style={styles.container}>
      <View style={styles.mapContainer}>
        <View style={styles.mapView}>
          <MapView
            style={styles.map}
            initialRegion={{
              latitude: info.mapData.latitude,
              longitude: info.mapData.longitude,
              latitudeDelta: 0.0015,
              longitudeDelta: 0.0015
            }}
          >
            <MapView.Marker
              key={state.params.id}
              coordinate={{
                latitude: info.markerData.latitude,
                longitude: info.markerData.longitude
              }}
              title={info.markerData.location}
              description={info.markerData.comments}
              onDragEnd={(e) => {
                info.markerData.latitude = e.nativeEvent.coordinate.latitude;
                info.markerData.longitude = e.nativeEvent.coordinate.longitude;
                console.log("New" + info.markerData.latitude + "...." + info.markerData.longitude)
              }
              }
            >
            </MapView.Marker>
          </MapView>
        </View>
      </View>

      <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 }}>
        <View style={{ flex: 0.5, height: 1, backgroundColor: '#003f5c' }} />
        <View>
          <Text style={{ color: '#003f5c', width: 100, textAlign: 'center', fontWeight: 'bold', letterSpacing: 1, fontSize: 18 }}>DETAYLAR</Text>
        </View>
        <View style={{ flex: 0.5, height: 1, backgroundColor: '#003f5c' }} />
      </View>

      <SafeAreaView style={styles.infoContainer}>
        <ScrollView>

          <View style={styles.infoItem}>
            <View style={styles.infoLeft}>
              <Text style={styles.infoText}>Konteyner ID: </Text>
            </View>
            <View style={styles.infoRiht}>
              <Text style={styles.infoParam}>{info.markerData.id}</Text>
            </View>
          </View>

          <View style={styles.infoItem}>
            <View style={styles.infoLeft}>
              <Text style={styles.infoText}>Şehir: </Text>
            </View>
            <View style={styles.infoRiht}>
              <Text style={styles.infoParam}>{info.markerData.city}</Text>
            </View>
          </View>

          <View style={styles.infoItem}>
            <View style={styles.infoLeft}>
              <Text style={styles.infoText}>İlçe: </Text>
            </View>
            <View style={styles.infoRiht}>
              <Text style={styles.infoParam}>{info.markerData.district}</Text>
            </View>
          </View>

          <View style={styles.infoItem}>
            <View style={styles.infoLeft}>
              <Text style={styles.infoText}>Mahalle: </Text>
            </View>
            <View style={styles.infoRiht}>
              <Text style={styles.infoParam}>{info.markerData.street}</Text>
            </View>
          </View>

          <View style={styles.infoItem}>
            <View style={styles.infoLeft}>
              <Text style={styles.infoText}>Adres: </Text>
            </View>
            <View style={styles.infoRiht}>
              <Text style={styles.infoParam}>{info.markerData.adress}</Text>
            </View>
          </View>

          <View style={styles.infoItem}>
            <View style={styles.infoLeft}>
              <Text style={styles.infoText}>Bilgiler: </Text>
            </View>
            <View style={styles.infoRiht}>
              <Text style={styles.infoParam}>{info.markerData.info}</Text>
            </View>
          </View>
        </ScrollView>
      </SafeAreaView>
      
      <View style={styles.buttons}>
        <TouchableHighlight style={styles.leftB} onPress={()=>props.navigation.navigate('MarkerEdit', {
            id: info.markerData.id,
            adress: info.markerData.adress,
            street: info.markerData.street,
            district: info.markerData.district,
            city: info.markerData.city,
            country: info.markerData.country,
            info: info.markerData.info,
            latitude: info.markerData.latitude,
            longitude: info.markerData.longitude,
          })}>
          <View style={styles.ltouchable}>
            <FontAwesomeIcon icon={faEdit} color='white' size={30} />
          </View>
        </TouchableHighlight>

        <TouchableHighlight style={styles.rightB} onPress={() => { console.log('right button') }}>
          <View style={styles.rtouchable}>
            <FontAwesomeIcon icon={faTrashAlt} color='white' size={30} />
          </View>
        </TouchableHighlight>
      </View>
    </View>
  );
}

最后一个编辑标记:

import React, { useState } from 'react';
import { StyleSheet, View, Text, TextInput, SafeAreaView, ScrollView } from 'react-native';
import MapView from 'react-native-maps';

const MarkerEdit = (props) => {

    const { state } = props.navigation;
    console.log(state.params)

    const info = {
        markerData: {
            id: state.params.id,
            adress: state.params.adress,
            street: state.params.street,
            district: state.params.district,
            city: state.params.city,
            country: state.params.country,
            info: state.params.info,
            latitude: state.params.latitude,
            longitude: state.params.longitude,
        },
        mapData: {
            latitude: state.params.latitude,
            longitude: state.params.longitude,
            latitudeDelta: 0.015,
            longitudeDelta: 0.0121,
        },
    };
    return (
        <ScrollView style={styles.container}>
            <View style={styles.formRow}>
                <View style={styles.formColumn}>
                    <View style={styles.inputView} >
                        <TextInput
                            style={styles.inputText}
                            placeholder={info.markerData.id.toString()}
                            placeholderTextColor="#003f5c"
                            editable={false}
                        />
                    </View>
                    <View style={styles.inputView} >
                        <TextInput
                            style={styles.inputText}
                            placeholder={info.markerData.city}
                            placeholderTextColor="#003f5c"
                        />
                    </View>
                    <View style={styles.inputView} >
                        <TextInput
                            style={styles.inputText}
                            placeholder={info.markerData.street}
                            placeholderTextColor="#003f5c"
                        />
                    </View>
                </View>
                <View style={styles.formColumn}>
                    <View style={styles.inputView} >
                        <TextInput
                            style={styles.inputText}
                            placeholder={info.markerData.info}
                            placeholderTextColor="#003f5c"
                        />
                    </View>
                    <View style={styles.inputView} >
                        <TextInput
                            style={styles.inputText}
                            placeholder={info.markerData.district}
                            placeholderTextColor="#003f5c"
                        />
                    </View>
                    <View style={styles.inputView} >
                        <TextInput
                            style={styles.inputText}
                            placeholder={info.markerData.adress}
                            placeholderTextColor="#003f5c"
                        />
                    </View>
                </View>
            </View>
            <View style={{ flexDirection: 'row', alignItems: 'center', marginTop: 7 }}>
                <View style={{ flex: 0.5, height: 1, backgroundColor: '#003f5c' }} />
                <View>
                    <Text style={{ color: '#003f5c', width: 150, textAlign: 'center', fontWeight: 'bold', letterSpacing: 1, fontSize: 18 }}>SÜRÜKLE VE KONUM SEÇ</Text>
                </View>
                <View style={{ flex: 0.5, height: 1, backgroundColor: '#003f5c' }} />
            </View>
            <View style={styles.mapContainer}>
                <View style={styles.mapView}>
                    <MapView
                        style={styles.map}
                        initialRegion={{
                            latitude: info.mapData.latitude,
                            longitude: info.mapData.longitude,
                            latitudeDelta: 0.0015,
                            longitudeDelta: 0.0015
                        }}
                    >
                        <MapView.Marker
                            key={state.params.id}
                            draggable
                            coordinate={{
                                latitude: info.markerData.latitude,
                                longitude: info.markerData.longitude
                            }}
                            title={info.markerData.location}
                            description={info.markerData.comments}
                            onDragEnd={(e) => {
                                info.markerData.latitude = e.nativeEvent.coordinate.latitude;
                                info.markerData.longitude = e.nativeEvent.coordinate.longitude;
                                console.log("New" + info.markerData.latitude + "...." + info.markerData.longitude)
                            }
                            }
                        >
                        </MapView.Marker>
                    </MapView>
                </View>
            </View>
        </ScrollView>

    );
}
javascript reactjs react-native
2个回答
0
投票

看起来您正在使用反应导航。当您在导航时传递参数时,下一个屏幕不会将它们作为

state
中的
navigation
变量获取。

参数位于单独的 prop

route
上,该属性会传递到您导航到的屏幕中。

文档

即参数应如下所示:

// MarkerDetail
const { id, address, street... } = props.route.params;


0
投票

如果您使用最新的react-navigation作为导航库,那么从导航中获取参数的方式会发生变化,您可以使用以下方式获取导航参数

const {latitude} = props.route.params;

© www.soinside.com 2019 - 2024. All rights reserved.