-1

display user input value upon submit

In this code i saw from other thread/post here in Stackoverflow, ''''''' {submittedText && (

You just typed: {submittedText}

)}'''''


These are all my codes attached above... How would I enter text in input field/text box for many times? and put alert related to the text inputted. ex: Cat, dog, snake

The output below should be Student's name: cat (space) STudent's name: dog etc

Thank you!

1
  • your question is not clear Commented Oct 15, 2022 at 9:37

2 Answers 2

0

Your question is not clear.

If it means that you want to see the values entered in the past as well as the values entered now, you can use a string array.

import React, { useState } from "react";
import "./index.css";

export default function App() {
  const [enteredText, setEnteredText] = useState("");
  const [submittedText, setSubmittedText] = useState([]);

  const textChangeHandler = (i) => {
    setEnteredText(i.target.value);
  };

  const submitHandler = (event) => {
    event.preventDefault();
    setSubmittedText([...submittedText, enteredText]);
    setEnteredText("");
  };

  return (
    <div className="App">
      <h1>Get user input</h1>
      <form onSubmit={submitHandler}>
      <input
        placeholder="type something"
        type="text"
        value={enteredText}
        onChange={textChangeHandler}
      />
      <button type="submit" >
        Submit
      </button>
      </form>
      {submittedText.map(el => <p>You just typed: {el}</p>)}
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

6 Comments

These are all my codes attached above(will attach in few minutes)... How would I input in input field/text box many times? ex: Cat, dog, snake The output below should be Student's name: cat STudent's name: dog etc Code-----
{submittedText.map(el => <p>You just typed: {el}</p>)} this Line above is not working (the one with .map), it says "submittedText is not defined"
I can't give an exact answer because I don't know how well JSX works in react-native.
The code I wrote works fine in codesandbox. I checked again, and there seems to be no problem with the code.
I don't know if it's what you want, but I wrote it in react-native simply. Check it out and use it if you can. link
|
0

Your question is not clear ,maybe you are talking about execute code on field change So this might help

const Somename = () => {

const [formData, setFormData] = useState({
  username: ''
});  

  const handleFieldChange = e => {
  setFormData({
    ...formData,
    [e.target.name]: e.target.value
  });
};

return (
  <Form onSubmit={handleSubmit}>
    <Form.Group className="mb-3">
      {hasLabel && <Form.Label>Name</Form.Label>}
      <Form.Control
        placeholder={!hasLabel ? 'Name' : ''}
        value={formData.username}
        name="username"
        onChange={handleFieldChange}
        type="text"
      />
    </Form.Group>
 </Form>
  );
 };

1 Comment

Hi... Thankyou too for your help! Take care!! :)))))

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.