| 155 | * ``` |
| 156 | */ |
| 157 | export function useMutationState< |
| 158 | TResult = MutationState, |
| 159 | TMutation extends Mutation<any, any, any, any> = |
| 160 | MutationTypeFromResult<TResult>, |
| 161 | >( |
| 162 | options: MutationStateOptions<TResult, TMutation> = {}, |
| 163 | queryClient?: QueryClient, |
| 164 | ): Array<TResult> { |
| 165 | const mutationCache = useQueryClient(queryClient).getMutationCache() |
| 166 | const optionsRef = useRef(options) |
| 167 | const result = useRef<Array<TResult>>(null) |
| 168 | if (result.current === null) { |
| 169 | result.current = getResult(mutationCache, options) |
| 170 | } |
| 171 | |
| 172 | useEffect(() => { |
| 173 | optionsRef.current = options |
| 174 | }) |
| 175 | |
| 176 | return useSyncExternalStore( |
| 177 | useCallback( |
| 178 | (onStoreChange) => |
| 179 | mutationCache.subscribe(() => { |
| 180 | const nextResult = replaceEqualDeep( |
| 181 | result.current, |
| 182 | getResult(mutationCache, optionsRef.current), |
| 183 | ) |
| 184 | if (result.current !== nextResult) { |
| 185 | result.current = nextResult |
| 186 | notifyManager.schedule(onStoreChange) |
| 187 | } |
| 188 | }), |
| 189 | [mutationCache], |
| 190 | ), |
| 191 | () => result.current, |
| 192 | )! |
| 193 | } |