React Native 将callback作为参数传递给callback时出现错误,应为1个参数,但得到2个

qyuhtwio  于 2023-01-02  发布在  React
关注(0)|答案(2)|浏览(153)

我试图传递回调到一个设置状态,但它给我这个typescript错误=〉预期1参数,但得到2.ts(2554).我只是想传递一个回调到setState,以更新新对象的状态时,函数onVideoPlayed被调用.你能帮助我吗?谢谢你提前
下面是我代码:

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>
  );
}
7cwmlq89

7cwmlq891#

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); 
  }
bvjveswy

bvjveswy2#

试试看

setVideos(state=> {
    // Changes to the state
    return state;
})

相关问题