I am currently trying to remove from a list when I press a button. I have tried this:
const [currentSelection, setCurrentSelection] = useState([]);
const [removeSelection, setRemoveSelection] = useState([]);
function handleRemoveMeal() {
currentSelection = setCurrentSelection(currentSelection.filter((item)
=> !removeSelection.includes(item)));
}
but, I get a currentSelection is read-only
error. I am able to properly store all of the elements in both currentSelection and removeSelection, so I am a bit stuck. If I can get some help, I would much appreciate it!
Edit: I have also tried using splice, but my list does not update.
for (var i = currentSelection.length - 1; i >= 0; i--) {
for (var j = removeSelection.length - 1; j >= 0; j--) {
if (currentSelection[i] == removeSelection[j]) {
setCurrentSelection(currentSelection.splice(i, 1));
}
}
}
Edit 2: for more context
//meals to choose from
const meals = [
{ label: 'Bangkok Chicken Wrap', value: 21 },
{ label: 'Moo Shu Chicken', value: 25 },
{ label: 'Strawberry Gelatin', value: 43 },
{ label: 'Waffle Fries', value: 20 },
{ label: 'Firehouse Chili with Pork', value: 35 },
{ label: 'Gluten Free Cookies', value: 41 },
{ label: 'Pineapple Chunks', value: 6 },
{ label: 'Vegan Pub Fried Fish', value: 23 },
{ label: 'Brown Rice with Mushrooms', value: 47 },
]
const onFavSelectionsChange = favSelections => {
setRemoveSelection(favSelections);
}
<View>
<View style={ styles.selectMultipleView }>
<SelectMultiple
items={currentSelection}
selectedItems={removeSelection}
onSelectionsChange={onFavSelectionsChange}
/>
</View>
<View style={ [styles.buttonView, {alignItems:"center"}] }>
<Button style={ styles.removeButtonComponent } onPress= { handleRemoveMeal }>
<Text style={ styles.removeButtonText }>Remove</Text>
</Button>
</View>
</View>