Skip to content

Instantly share code, notes, and snippets.

@ohansemmanuel
Created April 13, 2019 09:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ohansemmanuel/4a60c0efd14d2380eeb942fb259026e3 to your computer and use it in GitHub Desktop.
const HoldStringVal = () => {
const textAreaEl = useRef(null);
const stringVal = useRef("This is a string saved via the ref object --- ")
const handleBtnClick = () => {
textAreaEl.current.value =
stringVal.current + "The is the story of your life. You are an human being, and you're on a website about React Hooks";
textAreaEl.current.focus();
};
return (
<section style={{ textAlign: "center" }}>
<div>
<button onClick={handleBtnClick}>Focus and Populate Text Field</button>
</div>
<label
htmlFor="story"
style={{
display: "block",
background: "olive",
margin: "1em",
padding: "1em"
}}
>
Prepare to see text from the ref object here. Click button above.
</label>
<textarea ref={textAreaEl} id="story" rows="5" cols="33" />
</section>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment