问题:我有一个包含TextInput和SectionList的页面。当我专注于TextInput然后单击SectionList中的ListItem时 - 它只隐藏键盘但不会导航到SecondPage,它应该在onPress上执行,如下所示。如果TextInput没有聚焦 - 一切正常。
期望的行为:关注TextInput,如果我点击ListItem - 它应该导航到SecondPage。
问题:
1.我的代码有问题,或者是SectionList的正常行为?
2.有没有办法达到预期的行为?
代码:
SectionList + TextInput组件
const ds = [
{ data: [ {word: 'BMW'}, {word: 'Mercedez'}, {word: 'Tesla'}], title: 'Cars' },
{ data: [{word: 'Finland'}, {word: 'USA'}, {word: 'Somalia'}], title: 'Countries' },
{ data: [{word: 'Water'}, {word: 'Coke'}, {word: 'Tea'}], title: 'Liquids' },
];
class FirstPage extends React.Component {
render() {
return (
<View>
<View style={{ flexDirection: 'row' }}>
<TextInput
onChangeText={(v) => this.setState({ text: v})}
style={{ borderWidth: 1, flex: 1 }}
value={this.state.text}
/>
</View>
<SectionList
renderItem={({ item }) => <ListItem word={item.word} navigation={this.props.navigation} />}
renderSectionHeader={({ section }) => <ListItem word={section.title} section/>}
sections={ds}
keyExtractor={(item) => item.word}
/>
</View>
);
}
}
ListItem组件
render() {
const { navigation, section, word } = this.props;
const { letterStyle, noteStyle, sectionStyle, termStyle } = styles;
if (section)
{
return (
<TouchableOpacity>
<View>
<CardSection style={sectionStyle}>
<Text style={letterStyle}>{word}</Text>
</CardSection>
</View>
</TouchableOpacity>
);
}
return (
<TouchableOpacity
onPress={() => navigation.navigate('SecondPage')}
>
<View>
<CardSection style={sectionStyle}>
<Text numberOfLines={1} style={termStyle}>{word}</Text>
</CardSection>
</View>
</TouchableOpacity>
);
}
答案 0 :(得分:0)
你试过这个:
onMenuPress(item){
console.log(item);
this.props.navigation.navigate("some_route");
}
render() {
.....some code
<ListItem onPress={(item) => this.onMenuPress(item)}>
....more code
}
请记住在构造函数上绑定onMenuPress方法,如下所示:
constructor(props){
super(props);
this.onMenuPress = this.onMenuPress.bind(this);
}
适合我。