【发布时间】:2018-05-30 01:21:53
【问题描述】:
我正在尝试从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);
【问题讨论】:
-
你能贴出
Flat_List的父代码/它的调用站点吗?props没有定义,很可能你没有准确地传递它们,但是如果我们可以看到父组件,则更容易调试。 -
其实这是parent,数据来自export default createContainer(params=>{ Meteor.subscribe('deputies'); return{ deputies: Meteor.collection('deputies').find( ), }; },Flat_List);
-
好的,您首先要确保
this.props.deputies在render被调用时到达。如果不是,则不会定义。这是你的第一步。您可以使用三元运算符this.props.deputies ? this.props.deputies : ["Data not arrived"]添加对数据到达的检查 - 如果它没有到达,它将在render is called时未定义。代码还有一些其他问题,但从那里开始! -
好的,我会试试的。我应该把表达式放在 render() 之后,就像 this.props.deputies 一样吗? this.props.deputies : ["数据未到达"] ?
-
是的,把它作为你的第一行放在
render和console.log之后就出来了。
标签: reactjs meteor react-native react-native-flatlist