Skip to content

Commit c30f476

Browse files
committed
Replace "useContext" with "state" in context ref
There is no point currying hooks because hook are by default available inside action creators
1 parent 082f373 commit c30f476

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export type Tail<T> = T extends Array<any>
2525
*/
2626

2727
type ContextReference<TState> = {
28+
state: TState
2829
setState: React.Dispatch<React.SetStateAction<TState>>
29-
useContext: <T>(context: React.Context<T>) => T
3030
}
3131

3232
type Action<TState, TArgs extends never[]= never[], TReturn = any> =
@@ -56,8 +56,8 @@ export default function createStateContext<TState, TActions extends Actions<TSta
5656
let [_state, setState] = React.useState(initialState)
5757
const useContext = React.useContext
5858
const _actions = mapActionsToDispatch({
59-
setState,
60-
useContext
59+
state: _state,
60+
setState
6161
}, actions)
6262

6363
const _store = { ..._state, ..._actions } as Store<TState, TActions>

tests/tests.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('Test function types', () => {
8484
const [context, Provider] = createContextState('empty state', {
8585
action: (reference) => {
8686
expect(reference).toHaveProperty('setState')
87-
expect(reference).toHaveProperty('useContext')
87+
expect(reference).toHaveProperty('state')
8888
verify()
8989
}
9090
})
@@ -109,21 +109,23 @@ describe('Test function types', () => {
109109
expect(setState).toBeInstanceOf(Function)
110110
verify()
111111
},
112-
action2: ({ useContext }) => {
113-
expect(useContext).toBeInstanceOf(Function)
112+
/*
113+
action2: ({ state }) => {
114+
expect(typeof state).toBeInstanceOf(Function)
114115
verify()
115116
},
117+
*/ // TODO: add this test
116118
})
117119

118120
const Consumer: React.FC = props => {
119121
const store = React.useContext(context)
120122
store.action1()
121-
store.action2()
123+
// store.action2()
122124
return <div />
123125
}
124126

125127
mount(<Provider><Consumer /></Provider>)
126-
expect(verify.mock.calls.length).toBe(2)
128+
expect(verify.mock.calls.length).toBe(1)
127129
})
128130

129131
it('can provide arguments to action', () => {
@@ -237,8 +239,8 @@ describe('Logic', () => {
237239
})
238240

239241
const [context2, Provider2] = createContextState({ state2: 'before' }, {
240-
action2: ({ useContext }) => {
241-
const store = useContext(context1)
242+
action2: (_) => {
243+
const store = React.useContext(context1)
242244
return store.action1()
243245
},
244246
})

0 commit comments

Comments
 (0)