【发布时间】:2022-10-07 16:12:23
【问题描述】:
在一个文件中,我有
export const useAlertMachine = () => {
const updateAlertsMutation = useUpdateAlerts();
return {
updateAlertsMutation
};
};
updateAlertsMutationhas statesisLoading、isSuccess、isIdle 和 isError。我希望能够在另一个文件中访问这些状态。例如,
import {useAlertMachine} from '+/machines/alertMachine'
const Alert = () => {
const {updateAlertsMutation} = useAlertMachine();
// want to access updateAlertsMutation.isLoading here, referring to the mutation defined in the first file
}
现在,updateAlertsMutation 的每个实例都是独立的——是否可以跨文件访问其状态?
【问题讨论】:
-
我认为最好的方法是使用上下文或者在树中向上传递给父组件,但似乎有一个hack:tanstack.com/query/v4/docs/guides/mutations#persist-mutations,我不知道你的用例是否是这个功能的初衷,但是......如果你真的想要似乎你可以使用它。
标签: javascript reactjs react-query xstate