Skip to content

Commit

Permalink
fix: function component doesn't support ref
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Apr 13, 2022
1 parent 6151a5a commit 5490d6c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"husky": "^7.0.4",
"ts-prepublish": "^2.0.0",
"typescript": "^4.5.4"
},
"dependencies": {
"hoist-non-react-statics": "^3.3.2"
}
}
15 changes: 9 additions & 6 deletions src/hoc.tsx
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);
}

0 comments on commit 5490d6c

Please sign in to comment.