0

I mapped option select data from the Api but i have problem to change value of select input i use this handler change but the input can't change value is always fix what is the error in my code? how can i change the input value My error is like this is look please in the link video

https://github.com/JedWatson/react-select/issues/796

All the code in the same component

initialValues


const initialStates = {
         rug: {nom: "", id: "" }
    }


    const [currentStates, setCurrentStates] = useState(initialStates);

handle change methode



    const handleChange = event =>{
        setCurrentStates({ ...rugList[event.target.value].drug.id });
    };

Select input



  <Form.Group as={Col} controlId="formGridDrug">
                                                        
<Form.Label> Select</Form.Label>
                                                       
 <Form.Control required as="select"
                                                          
  type="text"
                                                           
 id="rug.id"
                                                           
 name="rug.id"
                                                           
 value={currentStates.rug.id}
                                                           
 className={"bg-white text-dark"}
                                                           
 onChange={handleChange}
                                                      
  >
                                                         
   <option value="">Choice select</option>
                                                           
 {rugList.map((value) =>
                                                               
 <option value={value.id} key={value.id}>
                                                                  
  {value.nom}
                                                               
 </option>
                                                            
)}

                                                       
 </Form.Control>

                                                   
 </Form.Group>

6
  • where is initialStates initialized - is it outside this component? Commented Apr 6, 2021 at 22:50
  • No it is inside the component the error is in the handel change can’t work my error is like this issue look please in this link video github.com/JedWatson/react-select/issues/796 Commented Apr 6, 2021 at 23:11
  • you are linking an issue from react-select repo but it doesn't seem that you are using this library Commented Apr 6, 2021 at 23:20
  • No I don’t use it , but show you the video it is like my error ;( Commented Apr 6, 2021 at 23:23
  • you are using react-bootstrap right? Commented Apr 6, 2021 at 23:26

1 Answer 1

1

rugList is an array with objects with shape {id , nom }. your handleChange needs to find a an object at rugList with an id property that matches event.target.value to set currentStates correctly:

  const handleChange = event => {
      const rug = rugList.find(({ id }) => id === event.target.value);
      if(rug) setCurrentStates({ rug });
  };
Sign up to request clarification or add additional context in comments.

Comments

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.