| 135 | * ``` |
| 136 | */ |
| 137 | export function useMutationState< |
| 138 | TResult = MutationState, |
| 139 | TMutation extends Mutation<any, any, any, any> = |
| 140 | MutationTypeFromResult<TResult>, |
| 141 | >( |
| 142 | options: MutationStateOptions<TResult, TMutation> = {}, |
| 143 | queryClient?: QueryClient, |
| 144 | ): Array<TResult> { |
| 145 | const mutationCache = useQueryClient(queryClient).getMutationCache() |
| 146 | const optionsRef = useRef(options) |
| 147 | const result = useRef<Array<TResult>>(null) |
| 148 | if (result.current === null) { |
| 149 | result.current = getResult(mutationCache, options) |
| 150 | } |
| 151 | |
| 152 | useEffect(() => { |
| 153 | optionsRef.current = options |
| 154 | }) |
| 155 | |
| 156 | return useSyncExternalStore( |
| 157 | useCallback( |
| 158 | (onStoreChange) => |
| 159 | mutationCache.subscribe(() => { |
| 160 | const nextResult = replaceEqualDeep( |
| 161 | result.current, |
| 162 | getResult(mutationCache, optionsRef.current), |
| 163 | ) |
| 164 | if (result.current !== nextResult) { |
| 165 | result.current = nextResult |
| 166 | notifyManager.schedule(onStoreChange) |
| 167 | } |
| 168 | }), |
| 169 | [mutationCache], |
| 170 | ), |
| 171 | () => result.current, |
| 172 | )! |
| 173 | } |