Skip to content

Commit

Permalink
enhance: providerRef支持RefCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
awmleer committed Mar 22, 2021
1 parent b3bf3ee commit f2a50fb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reto",
"version": "0.9.3-alpha.0",
"version": "0.9.3",
"main": "index.js",
"repository": "https://github.com/awmleer/reto",
"description": "React store with hooks.",
Expand Down
10 changes: 7 additions & 3 deletions src/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import {forwardRef, MutableRefObject, PropsWithChildren, useImperativeHandle, useRef, useState} from 'react'
import {forwardRef, MutableRefObject, PropsWithChildren, Ref, useImperativeHandle, useRef, useState} from 'react'
import {Container} from './container'
import {Executor} from './executor'
import {getStoreContext, Store, StoreP, StoreV} from './store'
Expand All @@ -8,7 +8,7 @@ interface Props<S extends Store = Store> {
of: S
args?: StoreP<S>
memo?: boolean
storeRef?: MutableRefObject<StoreV<S>>
storeRef?: Ref<StoreV<S>>
}

export const Provider = function<S extends Store>(props: PropsWithChildren<Props<S>>) {
Expand All @@ -23,7 +23,11 @@ export const Provider = function<S extends Store>(props: PropsWithChildren<Props
const checkRef = useRef<CheckRef>()
function onChange(value: StoreV<S>) {
if (props.storeRef) {
props.storeRef.current = value
if (typeof props.storeRef === 'function') {
props.storeRef(value)
} else {
(props.storeRef as MutableRefObject<StoreV<S>>).current = value
}
}
checkRef.current.onInitialize()
}
Expand Down

0 comments on commit f2a50fb

Please sign in to comment.