在 react-native 中查找组件的特定子项
Finding the specific child of a component in react-native
我正在制作像这样的徽章或筹码
使用代码:
<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>
);
}
}
我正在制作像这样的徽章或筹码
使用代码:
<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>
);
}
}