使用React-Query进行重复搜索行为
我正在尝试使用React-Query进行以下操作:
我有一个模态对话框,其中包含一个搜索输入字段(旁边的搜索按钮)以及一堆其他表单字段。目标是使用户输入ID,然后单击搜索按钮时,请调用“搜索”端点,如果找到相应的实体,则使用此实体进行水合。
我想不出一种“自然感觉”的方式,可以用反应疑问。
我能想到的最好的内容如下:
const [searchId, setSearchId] = useState()
const getEntity = useGetEntity({ id: searchId }); // just a useQuery wrapping hook which uses enabled: !!id
useEffect(() => {
const found = getEntity.data
if (found) {
form.setFieldsValue(_.pick(found, ['name', 'address', 'country'])) // hydrate the form - I'm using antd here
setSearchId(null);
// otherwise the user wouldn't be able to repeat the search with the same id
}
}, [getEntity .data])
在哪里使用:
export const useGetEntity = ({ id }) => {
return useQuery({
queryKey: ['searchedEntity', id],
queryFn: () => fetchEntity(id), // axios call
enabled: !!id
})
}
...在哪里单击搜索按钮触发setSearchID(id)
,
但这感觉不佳。有更好的方法吗?
谢谢!
I'm trying to do the following with React-Query :
I have a modal dialog which contains a search input field (with a search button next to it) and a bunch of other form fields. The goal is to enable the user to enter an id and upon clicking the search button a 'search' endpoint is called and if a corresponding entity is found, the other form fields are hydrated using this entity.
I can't think of a 'natural feeling' way of doing this with React-Query.
The best I can come up with is as follows :
const [searchId, setSearchId] = useState()
const getEntity = useGetEntity({ id: searchId }); // just a useQuery wrapping hook which uses enabled: !!id
useEffect(() => {
const found = getEntity.data
if (found) {
form.setFieldsValue(_.pick(found, ['name', 'address', 'country'])) // hydrate the form - I'm using antd here
setSearchId(null);
// otherwise the user wouldn't be able to repeat the search with the same id
}
}, [getEntity .data])
where useGetEntity :
export const useGetEntity = ({ id }) => {
return useQuery({
queryKey: ['searchedEntity', id],
queryFn: () => fetchEntity(id), // axios call
enabled: !!id
})
}
...where clicking the search button triggers setSearchId(id)
But this feels off somehow. Is there a better approach ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理此问题的最佳方法是将搜索和表单分为两个不同的组件,并将数据作为道具传递给搜索表格,在该表单中可以将其用作本地状态的初始状态。
usegetentity
挂钩看起来很完美。,最好的方法是检查
searchID
的平等,然后触发<代码> refetch()从usequery
而不是设置ID,例如:The best way to approach this is to split the Search and the Form into two different components, and pass the data as a prop to the Search form, where it can be used as initial state for the local state. The
useGetEntity
hook looks perfect.if that is a requirement, the best way to do it is to check for equality of the
searchId
, and then triggerrefetch()
returned fromuseQuery
instead of setting the id, something like:React-Query
通常用于预先http调用。您可以简单地使用Axios
获取数据,然后将数据保存到您的状态,并将您的字段值引用到状态,以便任何状态变化都会导致重新渲染和补充您的表单字段。同样,在您的应用程序获取数据时,实现加载指示器也是一个很好的UX体验。
React-Query
is often used for advance http calls. you can simply fetch your data withaxios
and save the data to your state and also reference your field values to your state so that any state change leads to re-rendering and hydrating your form fields.Also implementing loading indicator is a good UX experience while your app is fetching data.