Skip to content

Commit ef7cf27

Browse files
authored
fix wrong return value (#12)
1 parent 9898838 commit ef7cf27

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.0.1
4+
5+
### Fix
6+
7+
- Fix `createUseContext` returned value instead hook
8+
39
## 2.0.0
410

511
### New features

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# create-use-context
22

3-
A helper method which wraps original React `useContext` method in a type-safe manner providing `NonNullable` context value. Will throw if used outside of `Provider`.
3+
A helper method which wraps original React `useContext` method in a type-safe manner providing `NonNullable` context value. Will throw if used outside of `Provider`
44

55
## Installation
66

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Yevgenii Mikhnytskyi",
33
"dependencies": {},
44
"type": "module",
5-
"description": "A helper method which wraps original React `createContext` method and provides additional functionality like wrapping a `Context.Provider`, `useContext` helper, etc.",
5+
"description": "A helper method which wraps original React `useContext` method in a type-safe manner providing `NonNullable` context value. Will throw if used outside of `Provider`",
66
"devDependencies": {
77
"@rollup/plugin-terser": "^0.4.0",
88
"@rollup/plugin-typescript": "^11.0.0",
@@ -43,5 +43,5 @@
4343
"dev": "rollup -c -w",
4444
"test": "echo \"Error: no test specified\" && exit 1"
4545
},
46-
"version": "2.0.0"
46+
"version": "2.0.1"
4747
}

src/createUseContext.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { useContext, Context } from 'react';
22

33
export function createUseContext<T>(context: Context<T>) {
4-
const value = useContext(context);
4+
return function() {
5+
const value = useContext(context);
56

6-
if (!value) {
7-
throw new Error(composeErrorMessage(context.displayName || 'Context'));
8-
}
7+
if (!value) {
8+
throw new Error(composeErrorMessage(context.displayName || 'Context'));
9+
}
910

10-
return value;
11+
return value;
12+
}
1113
}
1214

1315
function composeErrorMessage(name: string) {

0 commit comments

Comments
 (0)