使用React Native搜索栏

时间:2017-12-15 19:48:57

标签: reactjs meteor react-native react-native-flatlist

我试图从react-native-searchbar添加搜索栏来过滤列表,就像这样,但它会引发错误:

  代表没有定义。

我不知道自己应该从这里做什么,对不起,RN很新!数据来自流星应用程序。

class Flat_List extends Component{

  constructor(props){
    super(props);
    this._handleResults = this._handleResults.bind(this);
    this.state = { dataSource : { deputies } };
  }

    _handleResults(results){
    this.setState({dataSource: this.props.deputies(results)})
  }

   render(){
    const {deputies}= this.props; // the list is here

    return(
      <View>

        <SearchBar
          ref={(ref) => this.searchBar = ref}
          data={deputies}
          handleResults={this._handleResults.bind(this)}
          showOnLoad
          allDataOnEmptySearch
          hideBack
          autoCorrect= {false}
        />

        <List>
          <FlatList
            data={this.props.deputies}
            keyExtractor={(item)=> item._id}
            renderItem={({item})=>(
             <DeputyDetail deputy={item.depute} navigation={this.props.navigation} /> )} />
        </List>

      </View>
    );
  }
export default createContainer(params => {
  Meteor.subscribe('deputies');
  return { deputies: Meteor.collection('deputies').find() };
}, Flat_List);

2 个答案:

答案 0 :(得分:0)

您的州声明无效。在构造函数()中添加/编辑这些行:

const { deputies } = this.props;
this.state={
  dataSource: deputies
}

答案 1 :(得分:0)

constructor(props){

   super(props);
   this._handleResults = this._handleResults.bind(this);
   this.state={
      dataSource : {deputies} // Here -> You doesn't define deputies
   }
} 

仅在this.state之前添加此行:

const { deputies } = props // You don't need 'this' because 'props' is already in the constructor params.

或者你可以直接把它 - &gt; this.state{ dataSource: props.deputies }