Skip to content

Commit f2a50fb

Browse files
committed
enhance: providerRef支持RefCallback
1 parent b3bf3ee commit f2a50fb

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "reto",
3-
"version": "0.9.3-alpha.0",
3+
"version": "0.9.3",
44
"main": "index.js",
55
"repository": "https://github.com/awmleer/reto",
66
"description": "React store with hooks.",

src/provider.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import {forwardRef, MutableRefObject, PropsWithChildren, useImperativeHandle, useRef, useState} from 'react'
2+
import {forwardRef, MutableRefObject, PropsWithChildren, Ref, useImperativeHandle, useRef, useState} from 'react'
33
import {Container} from './container'
44
import {Executor} from './executor'
55
import {getStoreContext, Store, StoreP, StoreV} from './store'
@@ -8,7 +8,7 @@ interface Props<S extends Store = Store> {
88
of: S
99
args?: StoreP<S>
1010
memo?: boolean
11-
storeRef?: MutableRefObject<StoreV<S>>
11+
storeRef?: Ref<StoreV<S>>
1212
}
1313

1414
export const Provider = function<S extends Store>(props: PropsWithChildren<Props<S>>) {
@@ -23,7 +23,11 @@ export const Provider = function<S extends Store>(props: PropsWithChildren<Props
2323
const checkRef = useRef<CheckRef>()
2424
function onChange(value: StoreV<S>) {
2525
if (props.storeRef) {
26-
props.storeRef.current = value
26+
if (typeof props.storeRef === 'function') {
27+
props.storeRef(value)
28+
} else {
29+
(props.storeRef as MutableRefObject<StoreV<S>>).current = value
30+
}
2731
}
2832
checkRef.current.onInitialize()
2933
}

0 commit comments

Comments
 (0)