在自定义钩子中传递值给useQuery的对象数组,它变成空的?



我有一个问题希望你能告诉我我做错了什么以及如何解决它。

我试图在自定义钩子中传递一个对象数组给useQuery。

我做了一个控制台日志,数组中有值。但是当它进入调用axios的函数后,数组变为空。

代码如下:

//自定义钩子

const AddNewEvents = async (newEvents) => {
console.log('AddNewEvents fn: ', newEvents); //showing empty array
//return await axios.post(`${process.env.REACT_APP_API_URL}Event`, newEvents);
};
export const useAddNewEventsData = (newEvents, onSuccess, onError) => {
console.log('before arrow function: ', newEvents); //got values
return useQuery('add-new-events-query', () => AddNewEvents(newEvents), {
onSuccess: onSuccess,
onError: onError,
enabled: false,
});
};

//在App.js中

const [newEvents, setNewEvents] = useState([]);
const addNewEventsResp = useAddNewEventsData(
newEvents,
onSuccessUseAddNewEventsData,
onErrorUseAddNewEventsData
);

和我调用addneweventsrespr .refetch()后填充对象的neweevents数组。

我认为这是因为您没有在函数参数上传递neweevents。你能试试这个吗:

export const useAddNewEventsData = (newEvents, onSuccess, onError) => {
console.log('before arrow function: ', newEvents); //got values
return useQuery('add-new-events-query', (newEvents) => AddNewEvents(newEvents), { // here (newEvents) =>
onSuccess: onSuccess,
onError: onError,
enabled: false,
});

};

相关内容

  • 没有找到相关文章

最新更新