如何在reactjs的输入onchange事件中调用reactQuery refetch

how to call reactQuery refetch in input onchange event in reactjs

在我的 React 应用程序中,我需要调用 does exit API。 API 当输入中的更改事件发生时调用应该发生,我正在使用 reactQuery refetch 来做到这一点。

我试过下面的代码

const [createObj, setCreateObj] = useState(mCreateObj);

const { data: doexit, refetch: doexitRefetch } = useQuery('getDoexit', () => api.doexitAPI(createObj.c_name), { enabled: false });

const handleInput = ({ target: { name, value } }) => { setCreateObj(state => ({ ...state, [name]: value }), []); }

export const doexitAPI= (value) => axios.get(/确实存在/${值}, { headers: setHeader }).then(res => res);

useEffect(() => { console.log(createObj) doexitRefetch(); }, [createObj.mx_name])

如何调用input onchange事件

您可以使查询无效并使用查询键再次处理提取数据。

https://react-query.tanstack.com/guides/query-keys#if-your-query-function-depends-on-a-variable-include-it-in-your-query-key

const { data: doexit, refetch: doexitRefetch } = useQuery(['getDoexit',  createObj.mx_name], () => api.doexitAPI(createObj.c_name), { enabled: false });