使用代码:
<View style = {{flexDirection: 'row', marginLeft: 10, marginTop: 5}}>
{this.state.eduModes.map((v, i) => {
return (
<Badge
key={i}
onPress = {(i) => {
console.log(i);
}}
value={v}
containerStyle= {{marginRight: 5}}
textStyle={{ color: 'orange' }}
/>
)
})}
</View>
用户从创建徽章的选择器中选择值,现在我想要的是当用户点击徽章时应该删除徽章。那么我如何访问用户点击的特定徽章,以便它在重新呈现时消失?
最佳答案
您可以创建一个新的内联函数,将应删除的徽章索引发送到删除函数。
示例
class App extends React.Component {
handlePress = index => {
this.setState(previousState => {
const eduModes = [...previousState.eduModes];
eduModes.splice(index, 1);
return { eduModes };
});
};
render() {
return (
<View style={{ flexDirection: "row", marginLeft: 10, marginTop: 5 }}>
{this.state.eduModes.map((v, i) => {
return (
<Badge
key={i}
onPress={() => this.handlePress(i)}
value={v}
containerStyle={{ marginRight: 5 }}
textStyle={{ color: "orange" }}
/>
);
})}
</View>
);
}
}
关于javascript - 在 react-native 中查找组件的特定子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51586876/