1

I got this error message when trying to embed react-native-maps functions.

react-native-maps - TypeError: undefined is not an object (evaluating '_this.state.region')

My code

Home.js

import MapView from 'react-native-maps';

export default () => (
    <MapView 
    style={{
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
    }} 
    region={this.state.region} 
    onRegionChange={this.onRegionChange}
  />
);

index.js

export default class App extends React.Component {

/**/

  getInitialState() {
    return {
      region: {
        latitude: 37.78825,
        longitude: -122.4324,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      },
    };
  }

  onRegionChange(region) {
    this.setState({ region });
  }

  render() {
    /**/
  }
}

Reference: https://github.com/datomnurdin/auth-reactnative

3 Answers 3

0

First of all getInitialState is deprecated. Check this

and working example. https://snack.expo.io/@nazrdogan/mapview-example?session_id=snack-session-cSA-6uzo9

Second problem is onRegionChange function. you need to bind or use arrow function

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have state in the context you are using it. i.e. the function in Home.js

What you should do is pass the region from your component as a prop in your Map stateless component.

Moreover, the onRegionChange is also not available here so pass that too as a prop

Your code should look like :

import MapView from 'react-native-maps';

 export default ({region, onRegionChange}) => (
   <MapView 
     style={{
       position: 'absolute',
       top: 0,
       left: 0,
       right: 0,
       bottom: 0,
     }} 
     region={region} 
     onRegionChange={onRegionChange}
    />
    );

and when you use this in rendering use it like below from your component where you have the region state variable.

import MyMap from './Home' . //Assuming that the Map function is imported as MyMap name.
....
render() {
  ...
   <MyMap region ={this.state.region} onRegionChange ={this.onRegionChange} />
}

Also, you should not use getInitialState as its deprecated and should not be used with the class syntax.

You should initialize your state like this:

export default class App extends React.Component {
   constructor (props) {
      super (props);
      this.state = {
       ...
       region: {
           latitude: 37.78825,
           longitude: -122.4324,
           latitudeDelta: 0.0922,
           longitudeDelta: 0.0421,
      },
     }
   }
}

Also, you have not binded your onRegionChanged function in your class, do it using ES6 => operator.

onRegionChange = (region) => {
   this.setState({ region });
}

3 Comments

I already tried, no error message so far but it seems not to detect latitude & longitude for a map region.
@MohammadNurdin Can you verify that you are having region object with data before rendering in map function ?
how to verify it? i try this but nothing happened. onRegionChange = (region) => { alert(region); this.setState({ region }); }
-1

I already got the answer. Create a new file to declare a new global variable and function.

Home.js

import MapView from 'react-native-maps';
import { onRegionChange, _values } from "./NewFile";

export default () => (
    <MapView 
    style={{
        position: 'absolute',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
    }} 
    region={_values.region}
    onRegionChange={() => onRegionChange() }
  />
  />
);

NewFile.js

export const _values = { 
  region: {
        latitude: 37.78825,
        longitude: -122.4324,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      }
};

export const onRegionChange = (region) => {
     _values.region = region;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.