我有过滤屏幕和搜索屏幕。当用户从搜索屏幕导航时,我将搜索词从搜索屏幕传递到主屏幕。类似地,我正在为过滤器屏幕做同样的事情(将filterOptions从过滤器屏幕传递到主屏幕)。
现在,当我从过滤器屏幕到主屏幕时,我想将searchTerm设置为null,同样如果我从搜索屏幕到主屏幕,我想将filterOptions设置为null我该怎么做?
在下面的代码中,行A和行B没有将filterOptions
和searchTerm
设置为null,这就是为什么过滤器和搜索不起作用,因为当filterOptions不为null时我希望searchTerm为null,这意味着只有过滤器应该在该实例上工作。类似地,如果filterOptions为null,则searchTerm不能为null,这意味着只有搜索才能在该实例上工作。
码:
filterProjectResults = (projects) => {
let filterOptions = this.props.navigation.getParam('filterOptions', null);
let searchTerm = this.props.navigation.getParam('searchTerm', null);
console.log('Initial filterOptions ', filterOptions); --> initially null
console.log('intital searchTerm ', searchTerm); --> initially null
if(filterOptions !== null){
console.log('Inisde filter screen ', filterOptions)
let display_filtered_projects = projects.filter((item) => item.floorplans.some(val => filterOptions.includes(val.bhk)));
searchTerm = null; <--- Line A
return display_filtered_projects;
} else if (searchTerm !== null) {
console.log('Inside Search screen ', searchTerm);
let search_filter_projects = projects.filter(
(project) => {
return project.name.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1
}
);
filterOptions = null; <<--- Line B
return search_filter_projects;
}
}
search.js:
<TouchableOpacity onPress={() => this.props.navigation.navigate('Projects', {searchTerm : this.state.text})}>
<Image source={require('../../assets/images/search.png')} style={{width: 24, height: 24, resizeMode: 'contain'}}/>
</TouchableOpacity>
filter.js:
<TouchableOpacity onPress={() => this.props.navigation.navigate('Projects', {filterOptions: this.state.filterOptions})}>
<View style={styles.filterButton}>
<Text style={styles.filterButtonText}>Apply Filters</Text>
</View>
</TouchableOpacity>
我在Flatlist中传递此过滤或搜索的数据以显示结果。如果我尝试使用状态实现它我会得到错误 - > https://imgur.com/a/P4vyRsu代码:https://gist.github.com/aditodkar/1df0175e2b13727daaae0373040f8dcf
你打电话的时候
this.props.navigation.navigate('Projects', {searchTerm : this.state.text})}>
你也可以像这样添加filterOptions: null
this.props.navigation.navigate('Projects', {searchTerm : this.state.text, filterOptions: null})}
?
然后对于另一个你可以打电话
this.props.navigation.navigate('Projects', {searchTerm : this.state.text, searchTerm: null})}
您也可以随时调用this.props.navigation.setParams(params)
并将它们设置为null。
希望有所帮助!