From what I understand you should never mutate an object inside an array directly in react native, however say that I want to update a specific object inside an array - but still keep the same index nr and position of that object, selectedGoal in this case. How would I do that?
Now it keeps adding it to the end, which makes the item always appear in the end when it's manipulated.
const [goals, setGoals] = useState([])
...other code here...
const updatedGoal = [...goals.filter(goal=>goal.id !== selectedGoal.id), selectedGoal]
setGoals(updatedGoal)
example of nested array:
const addTodoToGoal = (todo, goalListId) =>{
const relevantList = goals.find(list=> list.id === goalListId)
relevantList.todos.push(todo)
const goalsMinusRelevantList = goals.filter((list)=>list.id !== goalListId)
const newGoals=[...goalsMinusRelevantList, relevantList]
setGoals(newGoals)