Skip to content

Commit

Permalink
fix(#199 + #200): fix useRef for component root element
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Jun 19, 2022
1 parent e0896e6 commit ff3fef0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"htm": "^3.1.1",
"hundred": "^0.1.0",
"husky": "^8.0.1",
"jsdom": "^19.0.0",
"jsdom": "^20.0.0",
"lodash": "^4.17.21",
"np": "^7.6.1",
"ora": "^6.1.0",
Expand Down
85 changes: 43 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/react/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const createComponent = (fn: Function, props?: VProps, key?: string | nul
const component = hook(() => {
const ret = fn(props, key);
const newVNode = Array.isArray(ret) ? h('_', key ? { key } : {}, ...ret) : ret;
if (ret.ref) prevRef = ret.ref;
const ref = prevRef ?? { current: undefined };

// Handle nested components
Expand Down Expand Up @@ -39,9 +40,10 @@ export const createClass = (ClassComponent: typeof Component, props?: VProps) =>
let prevVNode: VNode | undefined;
const componentObject = new ClassComponent(props as VProps, null);
const rerender = () => {
const ret = componentObject.render(props) as VNode | undefined;
const newVNode = (Array.isArray(ret) ? h('_', {}, ...ret) : ret) as VNode;
const ret = componentObject.render(props) as any;
const newVNode = Array.isArray(ret) ? h('_', {}, ...ret) : ret;

if (ret.ref) prevRef = ret.ref;
const ref = prevRef ?? { current: undefined };
if (ref && ref?.current) {
patch(ref.current, newVNode, prevVNode);
Expand Down

0 comments on commit ff3fef0

Please sign in to comment.