0

I want to show more input when "married" option is selected. I tried this.

    const [single, setsingle] = useState(false);
    const [married, setmarried] = useState(false);
    
    <select class="form-select" aria-label="Default select example">
        <option
            value={single}
            name="single"
            onChange={(e) => {
                setsingle(e.target.value);
            }}
        >
            محرد
        </option>
        <option value={married} name="married"
            onChange={(e) => {
                setmarried(e.target.value)}}
        >
            متاهل
        </option>
    </select>

    {married === "married" ? <Wife /> : null}

What did I miss?

2 Answers 2

1

The easiest way to achieve this to manage the status in one variable. also, onChange event should be in select

import React, { useState } from "react";

export default function App() {
  const [status, setStatus] = useState(null);

  function changeStatus(e) {
    setStatus(e.target.value);
  }
  return (
    <div className="App">
      <select value={status} onChange={changeStatus}>
        <option value="single">Single</option>
        <option value="married">Maried</option>
      </select>
      {
        status === "married" && <Wife />
      }
    </div>
  );
}


const Wife = () => {
  return <div>
    <input placeholder="Please enter name" />
  </div>
}

Working sandbox - https://codesandbox.io/s/interesting-shamir-3k17b

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

Comments

0

There are few basics/fundamental things which you need to correct in your code:

  1. You need to put onChange inside <select>, not inside <option>.
  2. You have created single and married as boolean and then comparing it as string value.
  3. You are always setting the value false.
  4. You can't use class inside any JSX elements. Instead of class, you must use className for setting the CSS classes.

Your code should look like this:

const [single, setsingle] = useState(false);
const [married, setmarried] = useState(false);

...

return (
    <>
        <select className="form-select"
            aria-label="Default select example"
            onChange={e => {
                const selectedValue = e.target.value;
                if(selectedValue === "single") {
                    setsingle(true);
                    setmarried(false);
                } else {
                    setsingle(false);
                    setmarried(true);
                }
            }} 
        >
            <option value={"single"} name="single">
                محرد
            </option>
            <option value={"married"} name="married">
                متاهل
            </option>
        </select>
        {married && <Wife />} {/* here since married is boolean, you can directly use it like this, or you can use {married === true ? <Wife /> : null} */}
    </>
)

Comments

Your Answer

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