亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除 ReactNative(JS) 列表中的特定元素

如何刪除 ReactNative(JS) 列表中的特定元素

嗶嗶one 2023-03-18 16:43:43
我制作了一個待辦事項列表,無論我輸入什么都會被推送到一個數組中。當我映射數組時,我放了一個垃圾桶按鈕,他們可以在其中刪除“待辦事項”列表。我嘗試了Splice方法,但沒有用。例如,我有一個列表,上面寫著:“買牛奶”“拿雞蛋”如果我想刪除“Get Eggs”,它會刪除“Buy Milk”(它會刪除頂部的任何內容)。有人可以幫助我如何實現這一目標。這是我的代碼(React 本機代碼):removeList = (item) => {  let val = this.state.noteList;  let arr = val.splice(item, 1); // <= this is what I did but it is removing the first element of the list  let complete = this.state.completedTask;  complete.push(arr); this.setState({ arr})};這是我的可觸摸不透明度:<TouchableOpacity  onPress={this.removeList}  style={{    height: deviceHeight / 10,    width: deviceWidth / 6,    backgroundColor: "#e6a25c",    justifyContent: "center",  }}>  <AntDesign    name="checkcircleo"    size={45}    color="black"    style={{ alignSelf: "center" }}  /></TouchableOpacity>;這對你來說似乎是一個愚蠢的問題,但我似乎無法弄清楚。編輯:我試圖做一個平面列表而不是映射,但它對我不起作用。難道我做錯了什么:let newNote = [] // This is new NOTE and NOT THE COMPLETED SCREENnewNote.push(  <FlatList     data={this.state.noteList}         ListHeaderComponent={this.renderHeader}        renderItem={({item,index}) => {        <View style={{height:100,width:200,backgroundColor: "black"}}>           <View style={styles.newBoxView}>        <Text>{item}</Text>      </View>      </View>         }}  />  )
查看完整描述

2 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

我找到了我的答案,所以如果有人遇到同樣的問題,這里是代碼:


`deleteNote = (index) => {

console.log(index)

 let arr = this.state.noteList;

 delete arr[index]

 this.setState({

  activeCounter: Number(this.state.activeCounter - 1)

 })

}`

這是我的映射代碼:


  `this.state.noteList.map((item,index) => 

   <View style={styles.createBox}>  

   <View style={{flex:4,justifyContent: 'center',backgroundColor: 

   "lightpink",}}>

    <Text  style={{textAlign:'center',fontSize:deviceWidth/20,}}>

    {item.trim()}

  

  </Text>

</View>

<TouchableOpacity key={index} onPress={() => this.deleteNote(index)} style={{flex:1,justifyContent: 'center'}}>

<AntDesign  name="checkcircleo" style={{alignSelf:'center',backgroundColor: "#e6a25c"}} size={deviceWidth/5} color="black" />

</TouchableOpacity>

)`

我希望這有幫助。這實際上花了我 1 周的時間才弄清楚,最后我弄明白了。


查看完整回答
反對 回復 2023-03-18
?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

嘗試這個:


removeList = (item) => {

  let val = this.state.noteList;

  let arr;


  for (let i = 0; i < val.length; i++) {

    if (val[i] === item) {

      arr = val.splice(i, 1);

    }

  }


  let complete = this.state.completedTask;

  complete.push(arr);

};


查看完整回答
反對 回復 2023-03-18
  • 2 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號