简体   繁体   English

如何使用调用 API 的辅助函数在 UseEffect Hook 内部工作

[英]How to Use Helper Function that Calls an API to Work Inside of UseEffect Hook

I am new to ReactJS and am struggling on getting the useEffect to work properly.我是 ReactJS 的新手,正在努力让 useEffect 正常工作。 The below code works just fine:下面的代码工作得很好:

const [expenditureItems, setExpenditureItems] = useState([]);

    useEffect(() => {
    async function getExpenditureItems() {
      const financeUrl = "http://blah.blah.blah/blah";
      const filter = {"type": "Expenditure"}
      const response = await axios.post(financeUrl, filter);
      setExpenditureItems((Array.from(response.data)));
    }
    getExpenditureItems();
}, []);

However, I already have this same function defined in a helper file here and would rather not repeat code or have another place where the URL is defined:但是,我已经在这里的帮助文件中定义了相同的函数,并且不想重复代码或在另一个地方定义 URL:

import axios from 'axios';

const financeUrl = "http://blah.blah.blah/blah";
let response = ''

async function getDocuments(filter=null) {
if(!filter) {
    response = await axios.get(financeUrl);
}
else {
    response = await axios.post(financeUrl, filter);
}
console.log(response.data);
return Array.from(response.data);}

...More Stuff Here...

export {getDocuments, createDocument, updateDocument, deleteDocument}

I know the helper method itself is working correctly and the data returned looks exactly like the data that is returned without the use of the helper method.我知道辅助方法本身工作正常,返回的数据看起来与不使用辅助方法返回的数据完全一样。 Here is what I've tried so far based on what I read here: https://www.benmvp.com/blog/helper-functions-react-useeffect-hook/这是我迄今为止根据我在这里阅读的内容所尝试的: https ://www.benmvp.com/blog/helper-functions-react-useeffect-hook/

    useEffect(() => {
    const filter = {"type": "Expenditure"}
    setExpenditureItems(getDocuments(filter));
}, []);

I also tried this thinking maybe async was required in the useEffect since the underlying function was async:我也尝试过这个想法,因为底层函数是异步的,所以 useEffect 中可能需要异步:

    useEffect(() => {
    async function getExpenditureItems() {
      const filter = {"type": "Expenditure"}
      setExpenditureItems(getDocuments(filter));
    }
    getExpenditureItems();
}, []);

I tried using the useCallback method described in that link as well, but nothing seems to work.我也尝试使用该链接中描述的 useCallback 方法,但似乎没有任何效果。 I'm sure I'm missing something simple here or there is something I don't understand about how useEffect works that's preventing this from working.我确定我在这里遗漏了一些简单的东西,或者我不明白 useEffect 是如何工作的,这阻止了它的工作。 Any ideas?有任何想法吗?

The hint in your last attempt is that you made the function async , but nowhere in that function do you await anything.您上次尝试的提示是您创建了函数async ,但在该函数中没有任何地方await任何内容。

getDocuments returns a Promise . getDocuments返回一个Promise You need to await it:你需要await它:

useEffect(() => {
  async function getExpenditureItems() {
    const filter = {"type": "Expenditure"}
    const documents = await getDocuments(filter);
    setExpenditureItems(documents);
  }
  getExpenditureItems();
}, []);

Or even just:甚至只是:

useEffect(() => {
  async function getExpenditureItems() {
    setExpenditureItems(await getDocuments({"type": "Expenditure"}));
  }
  getExpenditureItems();
}, []);

Hey Chopps , looks like you need to await the getDocuments helper function, otherwise you are trying to set the state with a Promise .Chopps ,看起来您需要await getDocuments辅助函数,否则您正在尝试使用Promise设置状态。 In your first example it was not happening because you were already awaiting the Promise to be resolved const response = await axios.post(financeUrl, filter);在您的第一个示例中,它没有发生,因为您已经在等待解决Promise const response = await axios.post(financeUrl, filter);

useEffect(() => {
  const filter = {"type": "Expenditure"}
  setExpenditureItems(await getDocuments(filter));
}, []);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM