Skip to content

Commit

Permalink
improve forwardRef
Browse files Browse the repository at this point in the history
  • Loading branch information
mebtte committed Nov 22, 2023
1 parent 17e4d33 commit 18ebc5d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
14 changes: 2 additions & 12 deletions apps/pwa/src/components/input.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ForwardedRef,
forwardRef,
InputHTMLAttributes,
useImperativeHandle,
useRef,
} from 'react';
import { ForwardedRef, forwardRef, InputHTMLAttributes } from 'react';
import styled from 'styled-components';
import { ComponentSize } from '../constants/style';
import { CSSVariable } from '../global_style';
Expand Down Expand Up @@ -43,11 +37,7 @@ function Wrapper(
{ disabled = false, ...props }: Props,
ref: ForwardedRef<HTMLInputElement>,
) {
const inputRef = useRef<HTMLInputElement>(null);

useImperativeHandle(ref, () => inputRef.current!);

return <Input {...props} disabled={disabled} ref={inputRef} />;
return <Input {...props} disabled={disabled} ref={ref} />;
}

export default forwardRef<HTMLInputElement, Props>(Wrapper);
12 changes: 2 additions & 10 deletions apps/pwa/src/components/textarea.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ForwardedRef,
forwardRef,
TextareaHTMLAttributes,
useImperativeHandle,
useRef,
} from 'react';
import { ForwardedRef, forwardRef, TextareaHTMLAttributes } from 'react';
import styled from 'styled-components';
import { CSSVariable } from '../global_style';

Expand Down Expand Up @@ -41,9 +35,7 @@ function Wrapper(
{ disabled = false, ...props }: Props,
ref: ForwardedRef<HTMLTextAreaElement>,
) {
const textareaRef = useRef<HTMLTextAreaElement>(null);
useImperativeHandle(ref, () => textareaRef.current!);
return <Textarea {...props} disabled={disabled} ref={textareaRef} />;
return <Textarea {...props} disabled={disabled} ref={ref} />;
}

export default forwardRef<HTMLTextAreaElement, Props>(Wrapper);

0 comments on commit 18ebc5d

Please sign in to comment.