Fill the Select component only by clicking

Asked

Viewed 35 times

0

I’m with a project that when entering the component it carries the modal information, because the modal is with useEffect

useEffect(() => {
    buscarPlantas();
}, []);

But I want this list only to load when you click on the component.

I managed to put in the onFocus={buscarPlantas} component. Only it does not load the list into the component.

const [plantas, setPantas] = useState([]);

const buscarPlantas= () => {
    const plants = [];
    getBuscarPlantas().then((response) => {
        response.data.data.map(plant => (
            plants.push(plant.attributes)
        ))
    })
    setPlantas(plants);
}
<Select
    value={age}
    onChange={handleChange}
    displayEmpty
    className={classes.selectEmpty}
    inputProps={{ "aria-label": "Without label" }}
    onFocus={buscarPlantas}
>
    <MenuItem value="">
        <em className={classes.selectPlaceholder}>
            Selecionar plantas
        </em>
    </MenuItem>
    {
        plantas.map(planta => {
            return (
                <MenuItem key={planta} value={planta}>{planta}</MenuItem>
            )
        })
    }
</Select>

What may be missing ?

  • 1

    What library is this? react-select?

  • It does not seem a good idea to search when clicking, because the user will need to wait to load the list after clicking and you will need to give a load indicator so that it does not appear that the list is empty

  • You have no property onClick component? Returns no error, Warning, nothing?

  • The library is import Select from "@material-ui/core/Select";

  • Well, Atpe agree. But the idea of loading would be when opening the modal and not on the page that has the button that makes open the modal. Example. Property research besides bringing all the property still brings the plants. Since the plant will only serve if the user opens the modal. Got it @Rafaeltavares ?

  • @Shinforinpola No onclicck does not work. As said on 'onchange', Windows backend and returns, but does not show on screen.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.