I'm trying to add items to an array saved as a local state (using useState) through a function that is sent to another screen as a navigation param.
Assume i have ScreenA which looks:
ScreenA = () => {
const[obj,setObj] = useState({arr:[]})
addItem(item:any)=>{
setObj([...obj,arr:arr.concat(item)])
}
const objContext = {
obj,
addItem
}
return(
<objContext.Provider = value = {objContext}>
<ScreenB/>
<objContext/>
)
///
...
///
}
screenB = () => {
const {addItem} = useContext(Context)
return(
<View>
<Button onPress{()=>navigation.navigate("ScreenC",{addItemFunc:addItem})}
</Button>
</View>
)}
ScreenC = () => {
return(
<View>
<Button onPress={()=>route.params.addItemFunc(1)}/>
</View>
)}
export const Context = Tract.createContext({
obj: {},
addItem: (item: any) => null})
however while debugging, items are not getting added to the array, and only the last element added before returning to ScreenA is really added to the array.