Skip to content

Commit 8b171d1

Browse files
authored
Merge pull request #128 from pbeshai/103-no-context-warning
Add check for missing QueryParamProvider, update serialize-query-params for query-string version check
2 parents 5e9056a + 704966b commit 8b171d1

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"react-dom": ">=16.8.0"
8181
},
8282
"dependencies": {
83-
"serialize-query-params": "^1.2.1"
83+
"serialize-query-params": "^1.2.3"
8484
},
8585
"husky": {
8686
"hooks": {

src/LocationProvider.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ type LocationProviderContext = {
1717
) => void;
1818
};
1919

20-
export const LocationContext = React.createContext<LocationProviderContext>({
20+
const providerlessContextValue = {
2121
location: {} as Location,
2222
getLocation: () => ({} as Location),
2323
setLocation: () => {},
24-
});
24+
};
25+
26+
export const LocationContext = React.createContext<LocationProviderContext>(
27+
providerlessContextValue
28+
);
2529

2630
export function useLocationContext() {
2731
const context = React.useContext(LocationContext);
28-
if (process.env.NODE_ENV === 'development' && context === undefined) {
32+
if (
33+
process.env.NODE_ENV !== 'production' &&
34+
(context === undefined || context === providerlessContextValue)
35+
) {
2936
throw new Error('useQueryParams must be used within a QueryParamProvider');
3037
}
3138
return context;

src/__tests__/components-test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,18 @@ test('date withDefault', () => {
204204
getByText(/Change/).click();
205205
expect(queryByText(/x is 2020-06-06/)).toBeTruthy();
206206
});
207+
208+
test('error when no QueryParamProvider is used', async () => {
209+
const history = createMemoryHistory({ initialEntries: ['?x=3'] });
210+
211+
// silence the react console.error call
212+
let errMock = jest.spyOn(console, 'error').mockImplementation(() => {});
213+
expect(() =>
214+
render(
215+
<Router history={history}>
216+
<QueryParamExample />
217+
</Router>
218+
)
219+
).toThrow('useQueryParams must be used within a QueryParamProvider');
220+
errMock.mockRestore();
221+
});

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5283,10 +5283,10 @@ semver@^6.0.0, semver@^6.1.2, semver@^6.3.0:
52835283
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
52845284
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
52855285

5286-
serialize-query-params@^1.2.1:
5287-
version "1.2.1"
5288-
resolved "https://registry.yarnpkg.com/serialize-query-params/-/serialize-query-params-1.2.1.tgz#b849d0c004fb6958eabae114e24eb55aa8a50429"
5289-
integrity sha512-dycN0Cx4cXXIc3E3IaejBHlFWRRO0m/CzIkS41MhjQLwuvRobY1LHgdzPAfUj9sFeSnCmlMQM7iwVdyuhz2PIQ==
5286+
serialize-query-params@^1.2.3:
5287+
version "1.2.3"
5288+
resolved "https://registry.yarnpkg.com/serialize-query-params/-/serialize-query-params-1.2.3.tgz#0d6728c2378fa692233af80f4a72765db06f1069"
5289+
integrity sha512-pTssDTpnDR2p54q1/V1LpG/czg29iX9imxfKF1cupl30BWcRg8R8y3ddB4iHuhuVBO+HjSNj4+tAT1s075iimw==
52905290

52915291
set-blocking@^2.0.0:
52925292
version "2.0.0"

0 commit comments

Comments
 (0)