0

What I am trying to do is, upon clicking the Submit button, I want to grab the value that was typed into the input to then update the h1.

import React from "react";

function App() {
  const [headingText, setHeadingText] = React.useState("");

  function handleChange(event) {
    setHeadingText(event.target.value);
  }

  function handleSubmit(event) {
    alert(event.target);
  }

  return (
    <div className="container">
      <form onSubmit={handleSubmit}>
        <h1>Hello {headingText}</h1>
        <input
          onChange={handleChange}
          type="text"
          placeholder="What's your name?"
          value={headingText}
        />
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

export default App;

With the above code, so far I am able to update the h1 after every single keystroke that is entered into the input. But instead of that, I want to only update the h1 with the input value when I press the Submit button.

3 Answers 3

1

You'll need to use 2 useState's, one to hold the value of the input box, the other to hold the value of the header.

// import React from "react"; // not On StackOverflow

function App() {
  const [headingTextBuffer, setHeadingTextBuffer] = React.useState("");
  const [headingText, setHeadingText] = React.useState("");

  function handleChange(event) {
    setHeadingTextBuffer(event.target.value);
  }

  function handleSubmit(event) {
    event.preventDefault();
  
    setHeadingText(headingTextBuffer);
  }

  return (
    <div className="container">
      <form onSubmit={handleSubmit}>
        <h1>Hello {headingText}</h1>
        <input
          onChange={handleChange}
          type="text"
          placeholder="What's your name?"
          value={headingTextBuffer}
        />
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

// export default App; // not on StackOverflow

ReactDOM.render(<App />, document.querySelector("#root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much!! Going to try this now
I had a couple of typos, click "Run code snippet" to see in action
No worries--I noticed the typos--no big deal! Thx!
Btw, this also updates when you press enter/return on your keyboard due to the <form> tag
1

One way you could do this is using an inputText state that is updated in your handleChange function.

Now when you submit in your submit function you can update the headingText state to be equal to the inputText which should update your h1 value.

Comments

1

You could put the result of each keystroke in the state in another field that is not displayed anywhere and on click put the value from that new field into the heading. Like this :

import React from "react";

function App() {
  const [tempHeadingText, setTempHeadingText] = React.useState("");
  const [headingText, setHeadingText] = React.useState("");

  function handleChange(event) {
    setTempHeadingText(event.target.value);
  }

  function handleSubmit(event) {
    setHeadingText(tempHeadingText);
  }

  return (
    <div className="container">
      <form onSubmit={handleSubmit}>
        <h1>Hello {headingText}</h1>
        <input
          onChange={handleChange}
          type="text"
          placeholder="What's your name?"
          value={tempHeadingText}
        />
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

export default App;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.