1

My initial State

const [state,setState] = useState({
country : "",
nationality : "",
})

My first select tag :

<select> //select for country
<option>Nepal<option/>
<option>USA<option/>
</select>

My second select tag :

<select> //select for nationality 
<option>Nepali<option/>
<option>Ameracain<option/>
</select>

At the end , I need all the selected values of both select tag in the single state.

const [state,setState] = useState({
    country : "Nepal",
    nationality : "Nepali",
    })
1
  • 1
    This might be helpful. Click here! Commented Jul 25, 2021 at 2:29

2 Answers 2

1

You can use the name property and then use the multiple handle function in the React doc

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

export default function App() {
  const [state, setState] = useState({
    country: "Nepal",
    nationality: "Nepali"
  });
  const handleChange = (e) => {
    const {name,value}= e.target
 setState({...state,[name]:value})
    
  };
  return (
    <div className="App">
      <select name="country"  value={state.country} onChange={handleChange}>
        //select for country
        <option>Nepal</option>
        <option>USA</option>
      </select>
      My second select tag :
      <select name="nationality" value={state.nationality} onChange={handleChange}>
        //select for nationality
        <option>Nepali</option>
        <option>Americain</option>
      </select>
      {JSON.stringify(state)}
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

1

here is a sample based on your example

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

export default function App() {
  const [state, setState] = useState({
    country: "Nepal",
    nationality: "Nepali"
  });
  const handleChange = (e) => {
    const copy = { ...state };
    switch (e.target.name) {
      case "country":
        copy.country = e.target.value;
        break;
      case "nationality":
        copy.nationality = e.target.value;
        break;
      default:
        break;
    }
    setState(copy);
  };
  return (
    <div className="App">
      <select name="country" onChange={handleChange}>
        //select for country
        <option>Nepal</option>
        <option>USA</option>
      </select>
      My second select tag :
      <select name="nationality" onChange={handleChange}>
        //select for nationality
        <option>Nepali</option>
        <option>Ameracain</option>
      </select>
      {JSON.stringify(state)}
    </div>
  );
}

10 Comments

Thank you so much, it works but is is possible work with a single handle onchange??
I need your small help.. will you??
If I can, I will help.
Can we just get connected in the google meet ?? plz
Sorry I can't. You can send here.
|

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.