reactjs typescript 中的nextProps(功能组件)

rn0zuynd  于 2023-03-08  发布在  React
关注(0)|答案(1)|浏览(108)

我的 typescript 有问题,你能告诉我如何把useEffect钩子从“nextProps”(jsx)重写成 typescript (tsx)吗?

useEffect((nextProps) => {
    if (nextProps.selectedProviderJob !== selectedProviderJob) {
      setState({
        trackingEventOrder: "",
        nextNote: "",
        selectedSubpayers: [],
        approvalDate: null,
        followUpDate: null,
        isApproved: false,
        isPending: false,
        providerNumber: nextProps.selectedProviderJob.providerNumber,
      });
    }
  }, [props]);
j2qf4p5b

j2qf4p5b1#

useEffect钩子不获取任何返回类型,因此没有类型可设置。除此之外,在useEffect的第二个参数中使用props可能会导致繁重的工作量,如果有更多的props传递给组件,则会导致重载。因此,如果您只依赖于一个变量,我不建议您这样做。
我必须更正这个答案。useEffect-method有一个返回类型。你可以传递一个在组件被销毁时运行的方法。但是在你的情况下不需要。

useEffect(() => {[function that returns nothing]}, [variables which trigger the hook by change]);

相关问题