undefined is not an object (evaluating 'this.handleMapView.bind')

1.2k views Asked by At

I have been trying to binds methods to the class with no success. I think am doing everything right, as explained in the documentation. I have also tried other binding plugins but all in vain kindly help.

The weird thing is that, if I bind directly from the render method it works but the moment i try calling a method from another method hell brakes loose.

import React, { Component } from 'react';
import { Navigator, NetInfo, View } from 'react-native';
import { bindActionCreators } from 'redux';
import { connect  } from 'react-redux';
import { ActionCreators } from '../actions';
import _ from 'underscore';
import Api from '../utils/api';
import FooterView from '../components/footer';
import { Container, Content, Icon,
         Badge,Footer, FooterTab, Header,
         Title, Card, CardItem,Text,
         Spinner, List, ListItem, Tabs, Button } from 'native-base';
import theme from '../stylesheets/theme';
import Communications from 'react-native-communications';

function mapStateToProps(state) {
  return {
    currentUser: state.currentUserReducers,
  };
};
function mapDispatchToProps(dispatch) {
  return bindActionCreators(ActionCreators, dispatch);
};


class ClientDirectory extends Component {
  constructor(props){
    super(props);
  }

  renderCustomers(){
  let { currentUser, isLoading } = this.props;
  var customers = _.map(currentUser.customers, function(customer){
      return(
          <Card style={{padding: 1}}>
            <CardItem header>
                <Text>Shop Name: { customer.name }</Text>
            </CardItem>
            <CardItem cardBody>
                <Text>
                  Name: { customer.manager_first_name } { customer.manager_last_name }{"\n"}
                  Region: { customer.region_name }{"\n"}
                  Landmark: { customer.landmark }{"\n"}
                  Phone Number: { customer.phone_number }
                </Text>
            </CardItem>
            <CardItem style={{flex:1, alignItems: 'center', justifyContent: 'center', }} header>
                      <Button transparent style={{ flex:1 }} onPress={ () => Communications.phonecall(customer.phone_number, false)}><Icon name='ios-call' /></Button>
                      <Button transparent style={{ flex:1 }} onPress={ () => Communications.text(customer.phone_number)}><Icon name='ios-chatboxes'/></Button>
                      <Button transparent style={{ flex:1 }} onPress={ this.handleMapView.bind(this) }><Icon name='ios-compass' /></Button>
            </CardItem>
       </Card>
     );
   });
   return customers;
  }

  handleMapView(){
  let { navigator } = this.props;
   navigator.push({
    name: 'ViewOnMap',
   });
  }

  render(){
   return (
     <Container>
       <Header>
        <Button transparent>.</Button>
        <Title>Bonus client list</Title>
        <Button transparent>
            <Icon name='ios-menu' />
        </Button>
      </Header>
      <Content style={{padding: 10, marginBottom:10}}>
        { this.renderCustomers() }
      </Content>
    </Container>
  );
  }
 };

export default connect(mapStateToProps,mapDispatchToProps)(ClientDirectory);

just to mention, I have tried the below with no luck

http://moduscreate.com/using-es2016-decorators-in-react-native/

https://www.npmjs.com/package/autobind-decorator

Any help will be highly appreciated. Thanks.

2

There are 2 answers

2
Ben Shiundu On BEST ANSWER
0
Facundo La Rocca On

Move your bindings to the constructor like this:

constructor(props){
    super(props);

    this.handleMapView = this.handleMapView.bind(this);
}

And then your onPress callback should look like this:

onPress={ this.handleMapView }