在React中使用useseffect()和usecallback提取数据

发布于 2025-01-31 19:07:39 字数 2144 浏览 4 评论 0原文

问题

我正在寻找最佳的方法来使用useeffect()在多个地方使用fetch函数时。

当前情况

,我有一个父组件(itemContainer)和一个子组件(searchbar)。 itemContainer应使用getItemlist()函数获取所有可能的项目列表。我在第一个渲染过程中在usefect()中执行此功能,还将其传递到searchbar组件,以便当用户提交搜索词时,将在getItemlist()中更新itemList状态> itemcontainer

实际上,这正如我预期的那样工作。但是,我的问题是,

  1. 我不确定是否可以在这种情况下定义getItemlist() useeffect()。根据我一直在阅读的内容(博客帖子,官方文档),通常建议在usefeft()内定义数据获取功能,尽管可能会有一些边缘情况。我想知道我的案件是否适用于这种边缘情况。
  2. 可以在usecallback中将依赖项数组空留置吗?我尝试使用searchTermitemList填充它,但是它们都没有起作用 - 我很困惑为什么这样。

我不完全理解我编写的代码感到难过。如果有人能以我在这里缺少的东西来启发我...

ItemContainer

    const ItemContainer = () => {
        const [itemList, setItemList] = useState([]);
    
        const getItemList = useCallback( async (searchTerm) => {
            const itemListRes = await Api.getItems(searchTerm);
            setItemList(itemListRes)
        }, []);
    
        useEffect(() => {
            getItemList();
        }, [getItemList]);
    
        return (
            <main>
                <SearchBar search={getItemList} />
                <ItemList itemList={itemList} />
            </main>
        )
    }

搜索栏,我会很感激

const SearchBar = ({ search })  => {
    const [searchTerm, setSearchTerm] = useState('');

    const handleSubmit = (e) => {
        e.preventDefault();
        search(searchTerm);
        setSearchTerm('');
    }

    const handleChange = (e) => {
        setSearchTerm(e.target.value)
    }

    return (
        <form onSubmit={handleSubmit}>
            <input 
                placeholder='Enter search term...'
                value={searchTerm}
                onChange={handleChange}
            />
            <button>Search</button>
        </form>
    )
}

Issue

I'm looking for the most optimal way to fetch data using useEffect() when the fetch function is used in more than one place.

Situation

Currently, I have a parent component (ItemContainer) and a child component (SearchBar). ItemContainer should fetch the all the possible list of items using getItemList() functions. I'm executing this function within the useEffect() during the first render, and also passing it down to SearchBar component, so that when a user submits a search term, it will update itemList state by triggering getItemList() in ItemContainer.

This actually works just as I expected. However, my issue is that

  1. I'm not really sure whether it is okay to define getItemList() outside the useEffect() in this kind of situation. From what I've been reading (blog posts, react official docs) it is generally recommended that data fetching function should be defined inside the useEffect(), although there could be some edge cases. I'm wondering if my case applies as this edge cases.
  2. Is it okay to leave the dependency array empty in useCallback? I tried filling it out using searchTerm, itemList, but none of them worked - and I'm quite confused why this is so.

I feel bad that I don't fully understand the code that I wrote. I would appreciate if any of you could enlighten me with what I'm missing here...

ItemContainer

    const ItemContainer = () => {
        const [itemList, setItemList] = useState([]);
    
        const getItemList = useCallback( async (searchTerm) => {
            const itemListRes = await Api.getItems(searchTerm);
            setItemList(itemListRes)
        }, []);
    
        useEffect(() => {
            getItemList();
        }, [getItemList]);
    
        return (
            <main>
                <SearchBar search={getItemList} />
                <ItemList itemList={itemList} />
            </main>
        )
    }

SearchBar

const SearchBar = ({ search })  => {
    const [searchTerm, setSearchTerm] = useState('');

    const handleSubmit = (e) => {
        e.preventDefault();
        search(searchTerm);
        setSearchTerm('');
    }

    const handleChange = (e) => {
        setSearchTerm(e.target.value)
    }

    return (
        <form onSubmit={handleSubmit}>
            <input 
                placeholder='Enter search term...'
                value={searchTerm}
                onChange={handleChange}
            />
            <button>Search</button>
        </form>
    )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2025-02-07 19:07:39

这是我的答案。

  1. 是的,没关系。 usecallback内部的内容是“冷冻”的尊重
    可能发生的许多itemConteiner函数调用。自从
    usecallback content仅访问setItemlist,这也是一个
    冷冻处理程序,没有问题。

  2. 这也是正确的,因为空数组的意思是“依赖
    什么都没有”。换句话说,回调是一次创建的,并保留
    冻结itemContainer

相反,这很奇怪:

    useEffect(() => {
        getItemList();
    }, [getItemList]);

它有效,但几乎没有任何感觉。 getItemlist仅创建一次,那么为什么要根据某些事物从未改变?

使它变得更简单,仅运行一次:

    useEffect(() => {
        getItemList();
    }, []);

Here are my answers.

  1. Yes, it is okay. What's inside useCallback is "frozen" respect to
    the many ItemConteiner function calls that may happen. Since the
    useCallback content accesses only setItemList, which is also a
    frozen handler, there'll be no problems.

  2. That's also correct, because an empty array means "dependent to
    nothing". In other words, the callback is created once and keeps
    frozen for all the life of the ItemContainer.

Instead, this is something weird:

    useEffect(() => {
        getItemList();
    }, [getItemList]);

It works, but it has a very little sense. The getItemList is created once only, so why make an useEffect depending to something never changes?

Make it simpler, by running once only:

    useEffect(() => {
        getItemList();
    }, []);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文