0

This is the code .

const Details = () => {
const [counter, setCounter] = useState(1)

ques state for getting all the values of the form, and details state for storing all the submitions.

const [details, setDetails] = useState([])
const [ques, setQues] = useState({
    question: "",
    option1: "",
    option2: "",
    option3: "",
    option4: "",
    correct_option: ""
})

Handel change here.

const handelChange = (e) => {
    const name = e.target.name
    const value = e.target.value
    setQues({ ...ques, [name]: value })
}

Here I am submitting the form: first input was totaly blank in details

const handelSubmit = (e) => {
    e.preventDefault()
    setDetails([...details, ques])
    console.log(details)
    setQues({ question: "", option1: "", option2: "", option3: "", option4: "", correct_option: "" })
}

return (
    <div className="details-cont">
        <div className="details">

Form: required field is not working.

            <form >
                <div className="input_cont">
                    <label htmlFor="question">Question: {counter}</label>
                    <input required type="text" onChange={handelChange} value={ques.question} name="question" id="question" placeholder="Enter the question" />
                </div>
                <div className="input_cont">
                    <label htmlFor="option1">Option 1:</label>
                    <input type="text" required onChange={handelChange} value={ques.option1} name="option1" id="option1" placeholder="Enter the Option 1" />
                </div>
                <div className="input_cont">
                    <label htmlFor="option2">Option 2:</label>
                    <input type="text" required onChange={handelChange} value={ques.option2} name="option2" id="option2" placeholder="Enter the Option 2" />
                </div>
                <div className="input_cont">
                    <label htmlFor="option3">Option 3:</label>
                    <input type="text" required onChange={handelChange} value={ques.option3} name="option3" id="option3" placeholder="Enter the Option 3" />
                </div>
                <div className="input_cont">
                    <label htmlFor="option4">Option 4:</label>
                    <input type="text" required onChange={handelChange} value={ques.option4} name="option4" id="option4" placeholder="Enter the Option 4" />
                </div>
                <div className="toggle_cont">
                    <input className="correct_opt" required name="correct_option" type="number" onChange={handelChange} value={ques.correct_option} min="1" max="4" placeholder="correct option" />
                    <button onClick={handelSubmit} className="btn btn-add">Add</button>
                </div>
            </form>
            <button className="btn btn-create">Create</button>
        </div>
        <div className="d-left">
            Make your MCQ QUESTIONS on a GO!
        </div>
    </div >
)

}

Sai Charan
  • 31
  • 6
  • Duplicate: [Why calling react setState method doesn't mutate the state immediately?](https://stackoverflow.com/questions/30782948/why-calling-react-setstate-method-doesnt-mutate-the-state-immediately) –  Apr 30 '21 at 07:27
  • Regarding `required`, try `onSubmit={handleSubmit}` in the
    instead.
    –  Apr 30 '21 at 07:28
  • Please create a [mcve] in something like codesandbox – T J Apr 30 '21 at 07:29

1 Answers1

0

Actually native html validations are not going to work as long as you submit your form through <form> tag. Currently you are just submitting on click of button.

You can change it like:

<form onSubmit={handleSubmit}>
  {/* ... other fields ... */}
  <div className="toggle_cont">
     <input className="correct_opt" required name="correct_option" type="number" onChange={handelChange} value={ques.correct_option} min="1" max="4" placeholder="correct option" />
     <button type="submit" className="btn btn-add">Add</button>
   </div>
</form>
Muhammad Ali
  • 2,538
  • 2
  • 16
  • 21