Skip to content

Show previously selected data on the edit component select ( I use the same select component to add and to edit) #4642

Answered by ebonow
onasolani asked this question in Q&A
Discussion options

You must be logged in to vote

Greetings @onasolani,

You would need to use the Select as a controlled component, meaning that the selected value is kept in state and passed to the Select as a prop.

Since you would not be able to select the default hospital option until your list of hospitals or default id has come back from the api, you would likely want to create a useEffect hook to listen for changes to the options to know how/when to select the correct default value.

For example:

// assuming we have the id_Hospital passed in as a prop to the component
const { id_Hospital, handleChangeSelect } = props;

const [ hospitalOptions, setHospitalOptions ] = useState([]);
const [ hospitalValue, setHospitalValue ] = useState();

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@onasolani
Comment options

Answer selected by ebonow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants