Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix react warning #656

Merged
merged 9 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-squids-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'urql': patch
---

Avoid setting state on an unmounted component when useMutation is used
25 changes: 17 additions & 8 deletions packages/react-urql/src/hooks/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocumentNode } from 'graphql';
import { useState, useCallback } from 'react';
import { useState, useCallback, useRef, useEffect } from 'react';
import { pipe, toPromise } from 'wonka';

import {
Expand Down Expand Up @@ -31,6 +31,7 @@ export type UseMutationResponse<T, V> = [
export const useMutation = <T = any, V = object>(
query: DocumentNode | string
): UseMutationResponse<T, V> => {
const isMounted = useRef(true);
const client = useClient();

const [state, setState] = useState<UseMutationState<T>>(initialState);
Expand All @@ -46,18 +47,26 @@ export const useMutation = <T = any, V = object>(
),
toPromise
).then(result => {
setState({
fetching: false,
stale: !!result.stale,
data: result.data,
error: result.error,
extensions: result.extensions,
});
if (isMounted.current) {
setState({
fetching: false,
stale: !!result.stale,
data: result.data,
error: result.error,
extensions: result.extensions,
});
}
return result;
});
},
[client, query, setState]
);

useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);

return [state, executeMutation];
};
12 changes: 10 additions & 2 deletions packages/react-urql/src/hooks/useSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react-hooks/exhaustive-deps */

import { useMemo, useEffect, useState } from 'react';
import { useMemo, useEffect, useState, useRef } from 'react';

import {
Source,
Expand All @@ -19,6 +19,8 @@ import { useClient } from '../context';
let currentInit = false;
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved

export const useSource = <T>(source: Source<T>, init: T): T => {
const isMounted = useRef(true);

const [state, setState] = useState(() => {
currentInit = true;
let initialValue = init;
Expand All @@ -35,11 +37,17 @@ export const useSource = <T>(source: Source<T>, init: T): T => {
return initialValue;
});

useEffect(() => {
return () => {
isMounted.current = false;
}
})

useEffect(() => {
return pipe(
source,
subscribe(value => {
if (!currentInit) {
if (!currentInit && isMounted.current) {
setState(value);
}
})
Expand Down