I'm tring to pass a callback to a set state but it gives me this typescript error => Expected 1 arguments, but got 2.ts(2554). i just wanted to pass a callback to setState to update the satate of the new object when function onVideoPlayed is called . can you help me ? thank you in advance
here is my code:
const [videos, setVideos] = useState<Video[]>(videosList);
const [currentLanguage, setCurrentLanguage] = useState(i18n.language);
const navigation = useNavigation();
const renderItem = ({item, index}: {item: Video; index: number}) => {
return (
<Pressable onPress={() => onVideoPlayed(item, index)}>
<Card video={item} currentLanguage={currentLanguage} />;
</Pressable>
);
};
function onVideoPlayed(video: Video, index: number) {
if (video.viewedTimes && video.viewedTimes > 0) return;
let newVideos = videos;
newVideos[index].viewedTimes ;
setVideos(newVideos, () => {}); ===> "here it gives me the error"
}
return (
<View style={{flex: 1}}>
<FlatList
data={videos}
keyExtractor={item => item.title}
renderItem={({item, index}) => renderItem(item, index)}
numColumns={4}
contentContainerStyle={appStyles.cardListStyle}
/>
</View>
);
}
CodePudding user response:
function onVideoPlayed(video: Video, index: number) {
if (video.viewedTimes && video.viewedTimes > 0) return;
let newVideos = [...videos];
newVideos[index].viewedTimes = newVideos[index].viewedTimes 1;
setVideos(newVideos);
}
CodePudding user response:
Try
setVideos(state=> {
// Changes to the state
return state;
})