React Material UI Select not working properly

mynameismyname1010101

I am trying to populate the Select Component from the react Material UI library with options that I will then select and use for form data, I can populate the Select, but it won't let me choose any of the options! This is the code:

import React, { useState, useEffect } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Box from '@material-ui/core/Box';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import InputLabel from '@material-ui/core/InputLabel';
import { spacing } from '@material-ui/system';
import Card from '@material-ui/core/Card';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import { CardHeader, TextField, CircularProgress } from '@material-ui/core';
import { useDispatch, useSelector } from 'react-redux';
import { movieAction } from '../actions/movieActions';
import { IAppState } from '../store/store';
import axios from 'axios';

const GenreSelect = () => {

  const dispatch = useDispatch()

  const [genreChoice, setGenreChoice] = useState('')

  const getGenres = () => {
    console.log('actions dispatched')
    dispatch(movieAction())
  }

  const handleGenreChange = (event: React.ChangeEvent<{ value: unknown }>) => {
    setGenreChoice(event.target.value as string);
    console.log(event.target.value)
  };

  useEffect(() => {
    getGenres()
  }, [])

  const genres = useSelector((state: IAppState) => state.movieState.genres);
  
  const List = () => genres.map((genre: any) => {
    return <MenuItem key={genre.id} value={genre.id}>{genre.name}</MenuItem>
  })

  return (
    <>
    <InputLabel htmlFor="demo-simple-select-label">Genres</InputLabel>
    <Select
    labelId="demo-simple-select-label"
    id="demo-simple-select"
    value={genreChoice}
    onChange={handleGenreChange}
    >
      <List></List>
    </Select>
    </>
  )
}


const MovieForm = () => {
  return (
    <>
    <h1>Movie Suggester</h1>
    <Paper elevation={3}>
      <Box p={10}>
        <Card>
          <GenreSelect></GenreSelect>
          <Button onClick={() => console.log(axios.get(`https://api.themoviedb.org/3/discover/movie?api_key=${process.env.REACT_APP_MOVIE_API_KEY}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&with_genres=35&page=1`))}>Click me</Button>
        </Card>
      </Box>
    </Paper>
    </>
  )
}


export default MovieForm

For my first click on the select box it shows me all the options, logs an error in the console (screenshot below), and then when I try to click on the options it won't let me select any and doesn't trigger any console errors and just gives me the option-click ripple effect.

Here are two helpful screenshots: Select & data

Console.log error

Thanks for taking a look

Linda Paiste

The errors you are getting are about how the material-ui package uses ref forwarding. The Select component expects its children to be something which accepts a forwarded ref, and your List component doesn't.

I think that all you need to do is move the mapping inside of the Select rather than defining it as a separate function component.

  return (
    <>
    <InputLabel htmlFor="demo-simple-select-label">Genres</InputLabel>
    <Select
    labelId="demo-simple-select-label"
    id="demo-simple-select"
    value={genreChoice}
    onChange={handleGenreChange}
    >
      {(genres || []).map((genre: any) => {
         return <MenuItem key={genre.id} value={genre.id}>{genre.name}</MenuItem>
      })}
    </Select>
    </>

Now the children of Select are MenuItems without a wrapper around them.

Does that fix it?

Edit:

Replaced genres.map with (genres || []).map to avoid TypeError when the selector returns null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

react material-ui select not working

Material UI react select all children except for last child not working?

Select is not working onClick IconComponent(dropdown-arrow) in react material ui

React Material-UI Chip's onDelete inside Select is not working

Material-ui tooltip not working properly

Material UI textfield error not working properly in reactjs

WithStyles not working in Material UI for React

React Material UI Button component not working

React material UI autocomplete is not working with the formik

MenuItem with dynamic value is not working in Material UI React

Material-UI AppBar click is not working in react

React Router ScrollToTop not working with Material UI

React + Material UI + Typescript + Webpack + SSR not working

Material UI and React &hover selector not working

React, Material UI: Submit button not working on mobiles

React Material-UI Injecting withStyles not working

React Material-UI dropdown menu not working

React Material UI TextField FormHelperTextProps Styling Not Working

React Material UI TextField Styles Not Working

React material-ui KeyboardDatePicker and Formik not working

React Material-UI modal width not working

Passing props not working Material UI react

onChange of a Material UI TextField is not working (React)

Material UI react select value is not clearing on backspace

React render siblings on Multiple Select Material UI

Material UI Select component crashing React application

ReactJS & Material UI - ClickawayListener is not working properly in the Shadow DOM

state of material UI multi select doesn't update properly

How to properly add icon to select options in Material UI?