首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >绑定ListView时出错

绑定ListView时出错
EN

Stack Overflow用户
提问于 2018-05-30 06:18:05
回答 1查看 57关注 0票数 0

当我绑定我的listView时,我在我的渲染函数中得到一个错误。下面是错误的代码和屏幕截图:

我的代码如下。我正在尝试将搜索框放在列表视图的顶部。一切都很正常,但当我试图放置搜索框时,我开始收到上面的错误:

代码语言:javascript
复制
import React, { Component } from 'react';
import { Text, View, StyleSheet, ListView, TextInput} from 'react-native';
import { Provider, connect } from 'react-redux';
import { createStore } from 'redux'
import reducers from '../reducers/ServiceReducer';
import ServiceItem from './ServiceItem';
import Icon from 'react-native-vector-icons/EvilIcons';
import ServiceDetail from './ServiceDetail';

const styles = StyleSheet.create({


  separator: { 
        flex: 1, 
       height: StyleSheet.hairlineWidth, 
       backgroundColor: '#8E8E8E', 
       },

       text: {
        marginLeft: 12,
        fontSize: 16,
      },
      header_footer_style:{

        width: '100%', 
        height: 45, 
        backgroundColor: '#FF9800'

    },
    textStyle:{

      alignSelf:'center',
      color: '#fff', 
      fontSize: 18, 
      padding: 7

    },
    MainContainer:
    {
       justifyContent: 'center',
       flex:1,
       margin: 10

    },

    TextStyle:
    {
       fontSize: 23,
       textAlign: 'center',
       color: '#000',
    },



});


const store = createStore(reducers);

class AutoCompActivity extends Component {

  constructor(props) {

    super(props);

    this.state = {

      // Default Value of this State
      text: '',

    }

    this.arrayholder =[];
  }



  SearchFilterFunction(text){

    const newData = this.arrayholder.filter(function(item){
        const itemData = item.services.ser.toUpperCase()
        const textData = text.toUpperCase()
        return itemData.indexOf(textData) > -1
    })
    this.setState({
        dataSource: this.state.dataSource.cloneWithRows(newData),
        text: text
    })
}

  renderHeader = () => {

    var header = (

    <View style={styles.header_footer_style}>

      <Text style={styles.textStyle}> Tap the service to find the Loaction </Text>

    </View>

    );

    return header;

  };


  renderInitialView() {
    const ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2,
    });

    this.setState({
      dataSource : ds.cloneWithRows(this.props.services),
    }, function(){

      this.arrayholder=this.props.services;
    });




    if (this.props.detailView === true) {
      return (
        <ServiceDetail />
      );
    } else {
      return (
        <View style={styles.MainContainer}>

       <TextInput 
       style={styles.TextInputStyleClass}
       onChangeText={(text) => this.SearchFilterFunction(text)}
       value={this.state.text}
       underlineColorAndroid='transparent'
       placeholder="Search Here"
        />



        <ListView 
          enableEmptySections={true}
          dataSource={this.state.dataSource}
          renderSeparator={(sectionId, rowId) => <View key={rowId} style={styles.separator} />}
          renderHeader={this.renderHeader}
          style={{marginTop:10}}
          renderRow={(rowData) => 
            <ServiceItem services={rowData} />

          }
        />
        </View>
      );
    }
  }



  render() {
    return (
      <View style={styles.container}>
        {this.renderInitialView()}
      </View>
    );
  }
}



const mapStateToProps = state => {
  return { 

    services: state.services,
    detailView: state.detailView,
  };
};
const ConnectedAutoCompActivity = connect(mapStateToProps)(AutoCompActivity);


const app1 = () => (
  <Provider store={store}>
    <ConnectedAutoCompActivity />
  </Provider>
)

export default app1;

我的JSON (service.json)文件如下所示:

代码语言:javascript
复制
[


    {
        "id":0,
        "ser": "Test Service",
        "Location": "TestLoc",
        "Phone1":"(999)-999-5050",
        "SecondLoc": "TestLoc2",
        "email":"test@test.com",
        "sourceLat":"33.977806",
        "sourceLong":"-117.373261",
        "destLatL1":"33.613355",
        "destLongL1":"-114.596569",
        "destLatL2":"33.761693",
        "destLongL2":"-116.971169",
        "destAddr1": "Test Address, Test Drive",
        "destAddr2": "Test Address2, Test Drive2",
        "onlineURL":"",
        "Phone2": "(900)-900-3333"
      },
]

任何帮助都将受到高度的感谢。

EN

回答 1

Stack Overflow用户

发布于 2018-05-30 07:06:35

在构造函数中绑定函数:

代码语言:javascript
复制
this.SearchFilterFunction = this.SearchFilterFunction.bind( this );

或者使用箭头函数:

代码语言:javascript
复制
SearchFilterFunction = ( text ) => { ... };

关于绑定函数:https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50593784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档