-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: function component doesn't support ref
- Loading branch information
geekact
committed
Apr 13, 2022
1 parent
6151a5a
commit 5490d6c
Showing
2 changed files
with
12 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import { store } from 'foca'; | ||
import React, { ComponentType, useEffect, useState, forwardRef } from 'react'; | ||
import hoistStatics from 'hoist-non-react-statics'; | ||
import React, { ComponentType, useEffect, useState, FC } from 'react'; | ||
|
||
// TODO: 支持ref的类型 | ||
// 路由入口没有ref的需求,所以不需要增加forwardRef | ||
export function persistInterceptor<T>(EntryComponent: ComponentType<T>) { | ||
const [ready, setReady] = useState(false); | ||
const HOC: FC<T> = (props) => { | ||
const [ready, setReady] = useState(false); | ||
|
||
return forwardRef<unknown, T>((props, ref) => { | ||
useEffect(() => { | ||
store.onInitialized().then(() => { | ||
setReady(true); | ||
}); | ||
}); | ||
|
||
return ready ? <EntryComponent {...props} ref={ref} /> : null; | ||
}); | ||
return ready ? <EntryComponent {...props} /> : null; | ||
}; | ||
|
||
return hoistStatics(HOC, EntryComponent); | ||
} |