React Native无法过滤道具
问题描述:
我目前正在建立一个搜索栏,反应原生和redux但无法通过我的道具可以用文本输入过滤的格式。React Native无法过滤道具
onInput(text) {
this.setState({
text,
});
/* Search after user stop typing */
clearTimeout(this.inputDelay);
this.inputDelay = setTimeout(() => {
this.getResult(text);
}, 1000);
}
getResult(text) {
if (text.length > 0) {
const { entitiesList } = this.props;
const { dataSource } = this.state;
console.log(this.props.entitiesList); // returns object containing an array
const reg = new RegExp(text, 'i');
const entities = entitiesList.filter((entity) => {
return reg.test(entity.fullName);
});
this.setState({
dataSource: dataSource.cloneWithRows(entities),
});
}
Unhandled JS Exception: _this3.props.entitiesList.filter is not a function
被抛出。 This.props.entitiesList确实返回一个对象,所以我不知道我错了。
{ performances:
[ { _id: '5893b419d2b38b7122dc24f6',
fullName: 'Brett Monroe',
Z3_performance_Id: '696395',
A_relationship_Place: '1.',
N_event_Division: 'Varsity',
F_performance_Mark: '19.05',
C_athlete_Id: '273452',
},
{...},
{...}
]
}
任何提示/指针赞赏!
答
原因是entitiesList
是object
,你不能使用filter
或任何其他迭代器如map, forEach
等直接在object
。 entitiesList
包含密钥performances
,这是一个array
,所以你需要使用它是这样的:
const entities = entitiesList.performances.filter((entity) => {
return reg.test(entity.fullName);
});
+0
谢谢Mayank! – Chris
+0
很高兴,帮助你:) –
可以显示的'this.props.entitiesList'在控制台打印的价值? –
{演出: [{_id: '5893b419d2b38b7122dc24f6', L_section_Num: '1', N_impl_Division:空, 全名: '布雷特门罗', Z3_performance_Id: '696395', A_relationship_Place: '1', N_event_Division : '队打', F_performance_Mark:'11 .05a”, C_athlete_Id: 'A5273452', }, } .. – Chris