简体   繁体   English

将回调作为参数传递给回调给我错误预期 1 arguments,但得到 2

[英]Passing callback as parameter to callback give me error Expected 1 arguments, but got 2

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).我试图将回调传递给一组 state,但它给了我这个 typescript 错误 => 预期 1 arguments,但得到 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.我只是想将回调传递给 setState 以在调用 function onVideoPlayed 时更新新 object 的状态。 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>
  );
}
  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); 
  }

Try尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 给我一个错误:预期为 0 arguments,但得到 1 - Typescript is giving me an error: Expected 0 arguments, but got 1 在 Typescript 中传递 function 作为参数:预期为 0 arguments,但得到了 1.ts - Passing function as parameter in Typescript: Expected 0 arguments, but got 1.ts 打字稿错误-预期有1个参数,但有0个 - Typescript error - Expected 1 arguments, but got 0 typedoc / jsdoc文档通用参数(回调参数) - typedoc/jsdoc document generic parameter (callback arguments) 预期为 0 arguments,但在将 arguments 传递给 createAsyncThunk function 时得到 1 - expected 0 arguments, but got 1 while passing arguments to the createAsyncThunk function 打字稿错误预期0个参数,但在Ionic Framework中得到2个 - Typescript Error Expected 0 arguments, but got 2 in Ionic Framework Angular 4 错误“预期 2-3 个参数,但得到 1 个” - Angular 4 error “Expected 2-3 arguments, but got 1” Redux 工具包预期为 0 arguments,但出现 1 个错误 - Redux Toolkit Expected 0 arguments, but got 1 error 错误 TS2554:应为 2 arguments,但得到 1 - error TS2554: Expected 2 arguments, but got 1 角度可注射错误:预期有0个参数,但有1个 - Angular Injectable Error: Expected 0 arguments but got 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM