-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstrintToReact.tsx
More file actions
24 lines (23 loc) · 910 Bytes
/
Copy pathstrintToReact.tsx
File metadata and controls
24 lines (23 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import React, {useRef, useState, useEffect, type FC} from 'react';
import type {StringToReactComponentProps, IStringToReactApi, TBabel, TReact} from './types.d';
function StringToReactComponent(
deps: {
getCtx: (react: TReact, Babel: TBabel, rerender: (state: {}) => void) => IStringToReactApi;
react: TReact;
Babel: TBabel;
},
props: StringToReactComponentProps,
) {
const {getCtx, Babel, react} = deps;
const [, rerender] = useState<object>({});
let ref = useRef<IStringToReactApi | null>(null);
ref.current = ref.current || getCtx(react, Babel, rerender);
const api = ref.current as IStringToReactApi;
const data = props.data || {};
useEffect(() => {
api.update(props.children || '()=>null', props.babelOptions || {});
}, [props.children, props.babelOptions]);
const Com: FC = api.getComponent();
return <Com {...data} />;
}
export default StringToReactComponent;