在React中使用useseffect()和usecallback提取数据
问题
我正在寻找最佳的方法来使用useeffect()
在多个地方使用fetch函数时。
当前情况
,我有一个父组件(itemContainer
)和一个子组件(searchbar
)。 itemContainer
应使用getItemlist()
函数获取所有可能的项目列表。我在第一个渲染过程中在usefect()
中执行此功能,还将其传递到searchbar
组件,以便当用户提交搜索词时,将在getItemlist()
中更新itemList
状态> itemcontainer
。
实际上,这正如我预期的那样工作。但是,我的问题是,
- 我不确定是否可以在这种情况下定义
getItemlist()
useeffect()。根据我一直在阅读的内容(博客帖子,官方文档),通常建议在usefeft()
内定义数据获取功能,尽管可能会有一些边缘情况。我想知道我的案件是否适用于这种边缘情况。 - 可以在
usecallback
中将依赖项数组空留置吗?我尝试使用searchTerm
,itemList
填充它,但是它们都没有起作用 - 我很困惑为什么这样。
我不完全理解我编写的代码感到难过。如果有人能以我在这里缺少的东西来启发我...
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
- I'm not really sure whether it is okay to define
getItemList()
outside theuseEffect()
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 theuseEffect()
, although there could be some edge cases. I'm wondering if my case applies as this edge cases. - Is it okay to leave the dependency array empty in
useCallback
? I tried filling it out usingsearchTerm
,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的答案。
是的,没关系。
usecallback
内部的内容是“冷冻”的尊重可能发生的许多
itemConteiner
函数调用。自从usecallback
content仅访问setItemlist
,这也是一个冷冻处理程序,没有问题。
这也是正确的,因为空数组的意思是“依赖
什么都没有”。换句话说,回调是一次创建的,并保留
冻结
itemContainer
。相反,这很奇怪:
它有效,但几乎没有任何感觉。
getItemlist
仅创建一次,那么为什么要根据某些事物从未改变?使它变得更简单,仅运行一次:
Here are my answers.
Yes, it is okay. What's inside
useCallback
is "frozen" respect tothe many
ItemConteiner
function calls that may happen. Since theuseCallback
content accesses onlysetItemList
, which is also afrozen handler, there'll be no problems.
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:
It works, but it has a very little sense. The
getItemList
is created once only, so why make anuseEffect
depending to something never changes?Make it simpler, by running once only: