Skip to content

Commit

Permalink
🗃️ Forwarding Refs
Browse files Browse the repository at this point in the history
Forwarding Refs
  • Loading branch information
SaishJ committed Sep 8, 2022
1 parent 6eb606c commit 9a57ffb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import ParentComp from "./components/ParentComp";
import RefsDemo from "./components/RefsDemo1";
import RefDemo2 from "./components/RefDemo2";
import FocusInput from "./components/FocusInput";
import FRParentInput from "./components/FRParentInput";

function App() {
return (
<div className="App">
<FocusInput />
<FRParentInput />
{/* <FocusInput /> */}
{/* <RefsDemo /> */}
{/* <RefDemo2 /> */}
{/* <ParentComp /> */}
Expand Down
11 changes: 11 additions & 0 deletions src/components/FRInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

const FRInput = React.forwardRef((props, ref) => {
return (
<div>
<input type="text" ref={ref} />
</div>
);
});

export default FRInput;
24 changes: 24 additions & 0 deletions src/components/FRParentInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { Component } from "react";
import FRInput from "./FRInput";

export class FRParentInput extends Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
}

clickHandler = () => {
this.inputRef.current.focus();
};

render() {
return (
<div>
<FRInput ref={this.inputRef} />
<button onClick={this.clickHandler}>Click to Focus</button>
</div>
);
}
}

export default FRParentInput;

0 comments on commit 9a57ffb

Please sign in to comment.