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

Add example to reproduce race condition bug #90

Merged
merged 2 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions example/src/pages/UseAxiosPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UseAxiosChange,
UseAxiosRetry,
UseAxiosCustomInstance,
UseAxiosRaceCondition,
} from '../useAxios';

const UseAxiosPage: React.FC<RouteComponentProps> = () => (
Expand All @@ -16,6 +17,7 @@ const UseAxiosPage: React.FC<RouteComponentProps> = () => (
<UseAxiosChange />
<UseAxiosRetry />
<UseAxiosCustomInstance />
<UseAxiosRaceCondition />
</Flex>
);

Expand Down
48 changes: 48 additions & 0 deletions example/src/useAxios/RaceCondition.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useState } from 'react';
import Container from '../Container';
import Heading from '../Heading';
import TextBlock from '../TextBlock';
import { useAxios } from '../../../src';

interface Data {
data: {
name: string;
color: string;
};
}

export default () => {
const colors = {
CERULEAN: 1,
FUCHSIA: 2,
};
const [delay, setDelay] = useState(3000);
const [thing, setThing] = useState(colors.CERULEAN);

const { data, error, loading } = useAxios<Data>(
`http://slowwly.robertomurray.co.uk/delay/${delay}/url/https://reqres.in/api/things/${thing}`
);

setTimeout(() => {
setDelay(1000);
setThing(colors.FUCHSIA);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setTimeout should always be wrapped by a useEffect - what are you trying to do here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed setTimeout and wrapped the state changes in a useEffect

});

return (
<Container>
<Heading>useAxios with Race Condition</Heading>

<TextBlock>
{loading && 'Loading...'}
{error && error}
{data && !loading && (
<div>
{data.data.name}
{': '}
{data.data.color}
</div>
)}
</TextBlock>
</Container>
);
};
1 change: 1 addition & 0 deletions example/src/useAxios/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as UseAxiosCancel } from './Cancel';
export { default as UseAxiosChange } from './Change';
export { default as UseAxiosRetry } from './Retry';
export { default as UseAxiosCustomInstance } from './CustomAxiosInstance';
export { default as UseAxiosRaceCondition } from './RaceCondition';