Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 反应本机道具不保存到新阵列_Arrays_Loops_React Native_React Native Navigation_React Props - Fatal编程技术网

Arrays 反应本机道具不保存到新阵列

Arrays 反应本机道具不保存到新阵列,arrays,loops,react-native,react-native-navigation,react-props,Arrays,Loops,React Native,React Native Navigation,React Props,我将道具传递给嵌套组件。道具正确进入,但我无法将它们传递到这样的新阵列: import React, {useState, useEffect} from 'react'; import {StyleSheet, View} from 'react-native' import MapView, {Marker} from 'react-native-maps'; const Map = ({navigation, locationsOne, locationsTwo, location

我将道具传递给嵌套组件。道具正确进入,但我无法将它们传递到这样的新阵列:

import React, {useState, useEffect} from 'react';
import {StyleSheet, View} from 'react-native'
import MapView, {Marker} from 'react-native-maps';
    const Map = ({navigation, locationsOne, locationsTwo, locationsThree}) => { //these are coming in correctly when I console.log ie: {"coords":{"accuracy":602,"altitude":0,"heading":0,"latitude":99.4210083,"longitude":-100.0839934,"speed":0},"mocked":false,"timestamp":1572363100628}
        const [markers, setMarkers] = useState([
          {
            location : locationsOne,
            title: 'One'
          },
          {
            location : locationsTwo,
            title: 'Two'
          },
          {
            location : locationsThree,
            title: 'Three'
          }
        ]);
    return (
    <MapView style={styles.map} initialRegion={currentLocation.coords}>
      {markers.map((locationTEST) => (
        console.log('location: ' + JSON.stringify(locationTEST)) //this returns nothing in the locations arrays ie: {"location":[],"title":"One"}
         <MapView.Marker
           title="This is a title"
           description="This is a description"
           coordinate={{
             latitude: locationTEST.location.coords.latitude,
             longitude: locationTEST.location.coords.longitude,
           }}
         />
      ))}
</MapView>
);
import React,{useState,useffect}来自“React”;
从“react native”导入{样式表,视图}
从“react native maps”导入MapView,{Marker};
const Map=({navigation,locationsOne,locationsTwo,locationsThree})=>{//I console.log即:{“coords”:{“accurity”:602,“aights”:0,“heading”:0,“latitude”:99.4210083,“longitude”:-100.0839934,“speed”:0},“mocked”:false,“timestamp”:1572363100628}
常量[markers,setMarkers]=useState([
{
地点:地点,
标题:“一”
},
{
地点:地点二,
标题:“两个”
},
{
地点:地点三,
标题:“三”
}
]);
返回(
{markers.map((locationTEST)=>(
console.log('location:'+JSON.stringify(locationTEST))//这在locations数组中不返回任何内容,即:{“location”:[],“title”:“One”}
))}
);
locationsOne的值如下:
{“坐标”:{“精度”:602,“高度”:0,“航向”:0,“纬度”:99.4210083,“经度”:-100.0839934,“速度”:0},“模拟”:false,“时间戳”:1572363100628}

为什么我不能将道具传递到新的阵列(标记)阵列中?

当第一次呈现
时,
位置只有一个
仍然包含
[]

当道具(意味着
locationsOne
改变时,状态没有改变

所以,当道具发生变化时,您应该更新状态

const [markers, setMarkers] = useState ...

useEffect(() => {
  const newMarkers = [
    {
      location : locationsOne,
      title: 'One'
    },
    {
      location : locationsTwo,
      title: 'Two'
    },
    {
      location : locationsThree,
      title: 'Three'
    }
  ];
  setMarkers(newMarkers);
}, [locationsOne, locationsTwo, locationsThree]);
当第一次呈现
时,
位置One
仍然包含
[]

当道具(意味着
locationsOne
改变时,状态没有改变

所以,当道具发生变化时,您应该更新状态

const [markers, setMarkers] = useState ...

useEffect(() => {
  const newMarkers = [
    {
      location : locationsOne,
      title: 'One'
    },
    {
      location : locationsTwo,
      title: 'Two'
    },
    {
      location : locationsThree,
      title: 'Three'
    }
  ];
  setMarkers(newMarkers);
}, [locationsOne, locationsTwo, locationsThree]);

与其设置useEffect回调数组[](这会使系统陷入困境,因为每次更改位置时都会重新运行这些位置,这会导致位置被调用1400次),我只需要设置Markers(),如下所示:


与其设置useEffect回调数组[](这会使系统陷入困境,因为每次更改位置时都会重新运行这些位置,这会导致位置被调用1400次),我只需要设置Markers(),如下所示:


只看代码一件事。不使用
Map
作为组件名称可能不是一个好主意。
Map
是JavaScript中的OOB对象。@johnborges很好的一点,我更新了命名约定,但它仍然无法在整个过程中看到值function@Olivia我怀疑这是否与json有关skeleton。不太确定,但我看到的示例使用了相同的json SkeletonInterest。我刚刚使用useEffect进行了更新,并放入了回调数组-locationsOne、locationsTwo、locationsThree,我能够在一件事上取得一些进展,只需查看您的代码。不使用
映射作为组件名称可能不是一个好主意。
Map
是JavaScript中的OOB对象。@johnborges很好的一点,我更新了命名约定,但它仍然无法在整个过程中看到值function@Olivia我怀疑它是否与json框架有关。不太确定,但我看到的示例使用了相同的json SkeletonInterest。我刚刚用一个useEffec更新了它t并放入一个回调数组-locationsOne、locationsWO、locationsThree,我能够取得一些进展谢谢,这是对的,我现在能够看到位置值,但我关心递归。当我使用console.log时,我看到位置被加载了1400次。我的理解是useEffect应该在页面上只运行一次加载,然后[]允许我们查看道具何时更改。它们真的应该只更改一次,对吗?如果是这样,这是我理解页面如何通过导航加载的另一个问题。是否使用
useffect(()=>{…},[])
解决了您的问题?我认为您应该从使用
React.memo()中获益
相反,谢谢,这是正确的,我现在可以看到位置值,但我关心的是递归。当我使用console.log时,我看到位置被加载了1400次。我的理解是,useEffect应该只在页面加载时运行一次,然后在[]允许我们查看道具何时更改。它们真的应该只更改一次,对吗?如果是这样,这是我理解页面如何通过导航加载的另一个问题。是否使用
useffect(()=>{…},[])
解决了您的问题?我认为您应该从使用
React.memo()
中获益