如何处理返回陈旧事件处理程序的 React 钩子

时间:2020-12-31 09:24:36

标签: javascript reactjs react-hooks closures

我试图弄清楚如何处理由钩子返回的陈旧事件处理程序。首先,当组件首次呈现时,它会向 api 发出一个异步请求以获取凭据。然后在对话框中按下提交按钮以创建资源时使用这些凭据。问题是,即使在获取凭据后,对话框提交按钮单击事件处理程序的凭据也未定义。

credentials.js

import { useEffect } from 'react';
import { api } from './api';

export const useCredentials = (setCredentials) => {
  useEffect(() => {
    const asyncGetCredentials = async () => {
      const result = await api.getCredentials();
      if (result) {
        setCredentials(result);
      }
    };
    asyncGetCredentials().then();
  }, []);

  return credentials;
}

useComponent.js

import { useEffect, useRef, useCallback, useState } from 'react';
import { useCredentials } from './credentials';
import { createResource } from './resources';
import { useDialog } from './useDialog';

export const useComponent = () => {
  const { closeDialog } = useDialog();
  const [credentials, setCredentials] = useState();
  useCredentials(setCredentials);

  const credentialsRef = useRef(credentials);

  useEffect(() => {
    // logs credentials properly after they have been fetched
    console.log(credentials) 
    credentialsRef.current = credentials;
  }, [credentials]);

  const createResourceUsingCredentials = useCallback(
    async function () {
      // credentials and credentialsRef.current are both undefined
      // even when the function is called after the credentials
      // have already been fetched.
      console.log(credentials);
      console.log(credentialsRef.current);

      createResource(credentialsRef.current); 
    }, [credentials, credentialsRef, credentialsRef.current]
  );

  const onDialogSubmit = useCallback(
    async function () {
      await createResourceUsingCredentials();
      closeDialog();
    }, [
      credentials,
      credentialsRef,
      credentialsRef.current,
      createResourceUsingCredentials,
    ],
  );

  return {
    onDialogSubmit,
  }
}


3 个答案:

答案 0 :(得分:0)

试试这个方法

export const useCredentials = (setCredentials) => { 

  useEffect(() => {
    const asyncGetCredentials = async () => {
      const result = await api.getCredentials();
      if (result) {
        setCredentials(result);
      }
    };
    asyncGetCredentials().then();
  }, []); 
}


export const useComponent = () => {
  const { closeDialog } = useDialog();
  const [credentials, setCredentials] = useState(); // new add
  useCredentials(setCredentials); 

  ....
}

答案 1 :(得分:0)

为什么要增加复杂性,总是返回函数并检查函数内部是否有 credentials

export const useComponent = () => {
    const { closeDialog } = useDialog();
    const credentials = useCredentials();

    // correctly logs undefined at first and updated credentials
    // when they are asynchronously received from the api.
    console.log(credentials);

    async function createResourceUsingCredentials() {
        createResource(credentials);
    }

    let onClickDialogSubmit = async () => {
        if (credentials) {
            await createResourceUsingCredentials();
            closeDialog();
        }
    };

    return {
        onClickDialogSubmit,
    }
}

答案 2 :(得分:0)

我发现问题出在 useCredentials 钩子实现中。如果请求已经在进行中,它会阻止对 api 的任何进一步的凭据请求。由于此功能的实现不佳,如果渲染了使用该钩子的 1 个以上组件,则只有首先渲染的组件才能获得更新的凭据。我更改了 useCredentials 挂钩,以便它订阅全局状态(具有凭据),以便无论哪个组件启动请求,所有组件都将在请求完成时获得凭据。 https://simbathesailor007.medium.com/debug-your-reactjs-hooks-with-ease-159691843c3a 对调试此问题有很大帮助。