Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
How to change, using React, the value of a form input after clicking the submit button

I'm new to React. I was writing a simple form, using useState and useReducer hooks. After typing in the input field a number ex. 5, and submitting the form, my textField value shown on top of the HTML page equals 16. I would like this number to automatically be inserted as a new initial state or value in the input field. I tried to do this by typing: value={textField} but in that case I am not able to modify the input field on the HTML page.

import React, {useReducer, useState} from "react";

export default function App(){
    const [text, setText] = useState(0);

    console.log(text)
    function handleChange(e) {
        e.preventDefault();
        setText(e.target.value)
    }

    function onSubmit(e){
        e.preventDefault()
        setTextField()
    }

    const [textField, setTextField] = useReducer(

        (textField) => {
            textField = text;
            if (textField === 1){
                textField=1;
            }else if (textField%2 === 0){
                textField = textField/2
            }else if (textField%2 !== 0){
                textField = textField * 3 + 1;
            }
            return textField;
        }, 0);

    return (
        <div>
            <form onSubmit>
            <h1>{textField}</h1>
            <input type="text"
                    value={text}
                    onChange={handleChange}/>
                   <button className="button"
                           onClick={onSubmit}
                   >
                       Collatz it!
                   </button>
            </form>
        </div>
    )
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Before returning textField value from useReducer you can update the value of text using setText. Like this. Working Demo

import React, { useReducer, useState } from "react";

export default function App() {
  const [text, setText] = useState(0);

  console.log(text);
  function handleChange(e) {
    e.preventDefault();
    setText(e.target.value);
  }

  function onSubmit(e) {
    e.preventDefault();
    setTextField();
  }

  const [textField, setTextField] = useReducer((textField) => {
    textField = text;
    if (textField === 1) {
      textField = 1;
    } else if (textField % 2 === 0) {
      textField = textField / 2;
    } else if (textField % 2 !== 0) {
      textField = textField * 3 + 1;
    }
    setText(textField);
    return textField;
  }, 0);

  return (
    <div>
      <form onSubmit>
        <h1>{textField}</h1>
        <input type="text" value={text} onChange={handleChange} />
        <button className="button" onClick={onSubmit}>
          Collatz it!
        </button>
      </form>
    </div>
  );
}

about 4 years ago · Juan Pablo Isaza Report

0

I think giving up the reducer to make it more simple, and adding another local state for the "textField" number should solve it, like:

import React, { useState } from "react";

export default function App() {
  const [text, setText] = useState(0);
  const [textField, setTextField] = useState(0);

  console.log(text);
  function handleChange(e) {
    e.preventDefault();
    setText(e.target.value);
  }

  function onSubmit(e) {
    e.preventDefault();
    setTextField(getTextField(textField));
  }

  function getTextField(textField) {
    let result = 0;
    if (textField === 1) {
      result = 1;
    } else if (textField % 2 === 0) {
      result = textField / 2;
    } else if (textField % 2 !== 0) {
      result = textField * 3 + 1;
    }
    return result;
  };

  return (
    <div>
      <form onSubmit>
        <h1>{textField}</h1>
        <input type="text" value={text} onChange={handleChange} />
        <button className="button" onClick={onSubmit}>
          Collatz it!
        </button>
      </form>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Report

0

From what I understand, you just need to set your textField value in your text value right after the setTextField

The onSubmit function should look like this:

function onSubmit(e) {
  e.preventDefault();
  setTextField();
  setText(textField);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!