0

There was a problem showing the distance in onReady showing slower than render. Would like to show the distance and duration values ​​before displaying the render.

const MyMapView = (props) => {
    return (
        <MapView
            style={{ flex: 1 }}
            region={props.region}
            showsUserLocation={true}
        >
            <Marker coordinate={props.region} />
            <MapViewDirections
                origin={MapOrigin}
                destination={props.region}
                apikey={API_MAP.API}
                strokeWidth={3}
                strokeColor="hotpink"
                onReady={result => {
                    MapData.distance = result.distance
                    MapData.duration = result.duration
                    console.log(`Distance: ${MapData.distance} km`)
                    console.log(`Duration: ${MapData.duration} min.`)
                }}
            />
            <Body>
                <Text>distance : {MapData.distance} km.time : {MapData.duration} min</Text>
            </Body>
        </MapView>
    )
}

After displaying a value, it displays the value in Text before storing it in the const file.

Rajan
  • 1,512
  • 2
  • 14
  • 18
Ya'Su Min
  • 5
  • 1
  • 3
  • Can you please let me know what do you mean by **displaying the render**? – Rajan Mar 26 '20 at 11:32
  • I mean result.display and reult.duration it storage is slowly than display – Ya'Su Min Mar 26 '20 at 11:34
  • So you are not getting any result in **onReady**. Right? – Rajan Mar 26 '20 at 11:36
  • No. I mean storage val Map.distance = result.distance it a slowly than Instead of showing the new value, But showing the old value first. – Ya'Su Min Mar 26 '20 at 11:41
  • Is there a way to store variables and display more quickly than before? – Ya'Su Min Mar 26 '20 at 11:44
  • Where are you getting old Value in ` ` ? Or in `OnReady` – Rajan Mar 26 '20 at 11:57
  • After the display it shows, Before storing in variables **MapData.distance and MapData.duration** Therefore, in ` distance: {MapData.distance} km.time: {MapData.duration} min ` It shows the old values. When there is a change it doesn't show results. But after changing it again, it will show the other value that was collected before that round to show – Ya'Su Min Mar 27 '20 at 08:16

2 Answers2

0

Try to forceUpdate your component.

class MyMapView extends React.Component {
  render() {
    return (
      <MapView
        style={{ flex: 1 }}
        region={props.region}
        showsUserLocation={true}
      >
        <Marker coordinate={props.region} />
        <MapViewDirections
          origin={MapOrigin}
          destination={props.region}
          apikey={API_MAP.API}
          strokeWidth={3}
          strokeColor="hotpink"
          onReady={result => {
            MapData.distance = result.distance
            MapData.duration = result.duration
            console.log(`Distance: ${MapData.distance} km`)
            console.log(`Duration: ${MapData.duration} min.`)
            this.forceUpdate()
          }}
        />
        <Body>
          <Text>distance : {MapData.distance} km.time : {MapData.duration} min</Text>
        </Body>
      </MapView>
    )
  }
}
Rajan
  • 1,512
  • 2
  • 14
  • 18
0
onReady={result => {
 console.log(`Distance: ${result.distance} km`)
 console.log(`Duration: ${result.duration} min.`)
 this.forceUpdate()
}}
Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
GUGAN RAJ
  • 101
  • 7