使用React-Query进行重复搜索行为

发布于 2025-01-30 01:06:27 字数 983 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

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

发布评论

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

评论(2

暖风昔人 2025-02-06 01:06:27

处理此问题的最佳方法是将搜索和表单分为两个不同的组件,并将数据作为道具传递给搜索表格,在该表单中可以将其用作本地状态的初始状态。 usegetentity挂钩看起来很完美。

const [searchId, setSearchId] = useState()
const getEntity = useGetEntity({ id: searchId });

if (getEntity.data) {
  return <MyForm initialData={getEntity.data} />
}

// loading and error handling for the query goes her

//否则用户将无法使用相同的ID

重复搜索

,最好的方法是检查searchID 的平等,然后触发<代码> refetch()从usequery而不是设置ID,例如:

<button onClick={() => {
  (id === searchId) ? refetch() : setSearchId(id)
}}>Search</button>

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.

const [searchId, setSearchId] = useState()
const getEntity = useGetEntity({ id: searchId });

if (getEntity.data) {
  return <MyForm initialData={getEntity.data} />
}

// loading and error handling for the query goes her

// otherwise the user wouldn't be able to repeat the search with the same id

if that is a requirement, the best way to do it is to check for equality of the searchId, and then trigger refetch() returned from useQuery instead of setting the id, something like:

<button onClick={() => {
  (id === searchId) ? refetch() : setSearchId(id)
}}>Search</button>
高冷爸爸 2025-02-06 01:06:27

React-Query通常用于预先http调用。您可以简单地使用Axios获取数据,然后将数据保存到您的状态,并将您的字段值引用到状态,以便任何状态变化都会导致重新渲染和补充您的表单字段。

const [formData, setFormData] = useState({
  name: null,
  address: null,
  country: null
})
//////////

//call this for search
function getApiData(){
let data = await axios.get('URL');
setFormData({
  name:data.name,
  address:data.address,
  country:data.country
  })
}

同样,在您的应用程序获取数据时,实现加载指示器也是一个很好的UX体验。

React-Query is often used for advance http calls. you can simply fetch your data with axios 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.

const [formData, setFormData] = useState({
  name: null,
  address: null,
  country: null
})
//////////

//call this for search
function getApiData(){
let data = await axios.get('URL');
setFormData({
  name:data.name,
  address:data.address,
  country:data.country
  })
}

Also implementing loading indicator is a good UX experience while your app is fetching data.

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