import React, { forwardRef, useRef, render } from "react";
const Input = forwardRef((props, ref) => {
return <input {...props} ref={ref} />;
});
const Parent = () => {
const inputRef = useRef();
const focusInput = () => {
inputRef.current.focus();
};
return (
<div>
<h1>Using forwardRef with React 18</h1>
<Input
ref={inputRef}
placeholder="Type something..."
/>
<button onClick={focusInput}>
Focus the Input
</button>
</div>
);
};
render(<Parent />)
body, button {
font-size: 24px;
}
#root > div {
padding: 16px;
}
import React, { forwardRef, useRef, render } from "react";
const Input = ({ ref, ...props}) => {
return <input {...props} ref={ref} />;
};
const Parent = () => {
const inputRef = useRef();
const focusInput = () => {
inputRef.current.focus();
};
return (
<div>
<h1>Using forwardRef with React 19</h1>
<Input
ref={inputRef}
placeholder="Type something..."
/>
<button onClick={focusInput}>
Focus the Input
</button>
</div>
);
};
render(<Parent />)
body, button {
font-size: 24px;
}
#root > div {
padding: 16px;
}