【发布时间】:2018-09-29 04:37:05
【问题描述】:
我试图弄清楚如何捕获所有点击事件,以确定它们是否在我的 SearchBar 下拉菜单之外被点击。如果是这样,那么下拉菜单将关闭。我有一种检测所有点击事件的方法(TouchableWithoutFeedback),但我无法找到一种方法来比较或确定它是否在我的组件之外。有人知道如何在 React Native 中做到这一点吗?
class Products extends Component {
constructor(props) {
super(props);
this.hideSearchBar = this.hideSearchBar.bind(this);
}
hideSearchBar(e) {
console.log('e: ', e.nativeEvent.target)
// Determine if the click event was outside of the SearchBar component
}
render() {
const {isLoading, products} = this.props.products;
return (
<TouchableWithoutFeedback onPress={(e) => this.hideSearchBar(e)} style={{zIndex: 0}}>
<View style={styles.wrapper}>
<Header/>
<View style={styles.bodyWrapper}>
<ScrollView style={styles.scrollView}>
<ProductsContainer data={{productsList: { results: products }}}/>
</ScrollView>
<SearchBar ref={node => this.node = node} style={styles.searchBar}/>
</View>
<Footer/>
</View>
</TouchableWithoutFeedback>
);
}
}
【问题讨论】:
-
不同的环境(react 和 react-native),天哪,怎么可能是重复的?
标签: javascript reactjs react-native