0

我一直在尝试将方法绑定到类,但没有成功。正如文档中所解释的,我认为一切都是正确的。我也尝试过其他绑定插件,但都徒劳无功。

奇怪的是,如果我直接从渲染方法绑定它可以工作,但是当我尝试从另一个方法调用方法时,地狱刹车松了。

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);

顺便提一下,我尝试了以下方法,但没有成功

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

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

任何帮助将不胜感激。谢谢。

4

2 回答 2

0

这行得通。

var _this = this;

http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/

于 2016-12-19T14:07:15.790 回答
0

将绑定移动到构造函数,如下所示:

constructor(props){
    super(props);

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

然后您的 onPress 回调应如下所示:

onPress={ this.handleMapView }
于 2016-12-19T15:23:03.627 回答