javascript - 在 React Native 中按下按钮删除内容
问题描述
我的课堂上有以下内容:
constructor(props) {
super(props);
this.state={title: this.props.title, comment: '', units:'', rows: 1}
this.renderRows = this.renderRows.bind(this);
this.removeRow = this.removeRow.bind(this);
}
renderMore() {
let rowsAux = this.state.rows + 1;
this.setState({
rows: rowsAux
})
}
removeRow(i) {
console.log('selected row ' + i);
this.deleteById(i); // something like this???
}
renderRows() {
let max;
if (this.state.rows > 4) {
max = 4;
} else {
max = this.state.rows;
}
const options = [];
for (let i = 0; i < max; i++) {
let zI = 999 - i;
options.push(<View id='i' style={{ flexDirection: 'row', zIndex: zI}}>
<DropDownPicker
items={[
{label: 'Lunes', value: 'Lunes'},
{label: 'Martes', value: 'Martes'},
{label: 'Miércoles', value: 'Miércoles'},
{label: 'Jueves', value: 'Jueves'},
{label: 'Viernes', value: 'Viernes'}
]}
defaultValue={'Lunes'}
containerStyle={{height: 50}}
style={{marginTop: 5, backgroundColor: '#fafafa',width: 150,
borderTopLeftRadius: 10, borderTopRightRadius: 10,
borderBottomLeftRadius: 10, borderBottomRightRadius: 10}}
dropDownStyle={{backgroundColor: '#fafafa'}}
/>
<DropDownPicker
items={[
{label: '8 a 12hs', value: '8 a 12'},
{label: '12 a 16hs', value: '12 a 16'},
{label: '16 a 20hs', value: '16 a 20'}
]}
defaultValue={'8 a 12'}
containerStyle={{marginLeft: 10, height: 50}}
style={{marginTop: 5, backgroundColor: '#fafafa',width: 150,
borderTopLeftRadius: 10, borderTopRightRadius: 10,
borderBottomLeftRadius: 10, borderBottomRightRadius: 10}}
dropDownStyle={{backgroundColor: '#fafafa'}}
/>
<TouchableOpacity onPress={() => {this.removeRow(i)}}>
<Ionicons style={{marginTop: 12, marginLeft: 15}} name='ios-trash' color={'gray'} size={28}/>
</TouchableOpacity>
</View>);
}
return options;
}
我想要做的是在函数 renderRows 内的 IoniIcon ('ios-trash') 中添加一个 onPress 方法,该方法删除其对应的视图和那个视图。我怎样才能做到这一点?
更新:我的问题实际上是关于我应该如何实现我的 onPress 方法以指示它必须删除其父视图。我想也许我可以向父视图添加某种 ID,然后将其传递给 onPress 方法以删除该特定视图。
解决方案
将 Ionicons 包裹在 TouchableOpacity 中。
<TouchableOpacity onPress={}>
<Ionicons style={{marginTop: 12, marginLeft: 15}} name='ios-trash' color={'gray'} size={28}/>
</TouchableOpacity>
推荐阅读
- ios - 在 iOS SDK 4.38.0 中 Facebook 登录有时会失败
- c# - 即使条件为真,为什么我从 sql 查询中得到空结果?
- c# - 在VS winform中更改编辑模式下控件的Z顺序
- ios - 后台线程崩溃
- c++ - C++ 匹配括号解决方案
- gnu - GNU radio Stick 是否在硬件级别具有内置的高通滤波器,还是在设计模块时实现?
- python-sphinx - 版本替换在子文档中不起作用
- java - 如何使用 Selenium 向下滚动?
- ruby-on-rails - Rails 5 - 我可以在控制台中输入 ActiveRecord 代码来检查记录吗?
- oracle - PL/SQL Developer - ORA-28041 身份验证协议内部错误