Etika49
Etika49

Reputation: 742

Material UI Input Autocomplete not working in React.js

When I try to import Autocomplete exactly from the documentation: https://material-ui.com/components/autocomplete/ i get the following error message:

Failed to compile.

./node_modules/@material-ui/lab/esm/useAutocomplete/useAutocomplete.js
Attempted import error: 'unstable_useId' is not exported from '@material-ui/core/utils' (imported as 'useId').

react code:

/* eslint-disable no-use-before-define */
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';

export const MyTeamShiftPlanInput = () => {
  return (
    <Autocomplete
      id="combo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" variant="outlined" />}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },
  { title: 'The Godfather: Part II', year: 1974 },
  { title: 'The Dark Knight', year: 2008 },
  { title: '12 Angry Men', year: 1957 },
  { title: "Schindler's List", year: 1993 },
  { title: 'Pulp Fiction', year: 1994 },
  { title: 'The Lord of the Rings: The Return of the King', year: 2003 },
  { title: 'The Good, the Bad and the Ugly', year: 1966 },
  { title: 'Fight Club', year: 1999 }
];

current versions of MUI:

    "@material-ui/core": "^4.9.10",
    "@material-ui/lab": "^4.0.0-alpha.49",
    "@material-ui/pickers": "^3.2.10",

Upvotes: 1

Views: 5856

Answers (2)

yoges nsamy
yoges nsamy

Reputation: 1435

This is in addition to @Dhika's answer above.

His solution didn't work the first time I tried because I failed to notice there is no caret symbol in front of the version '"4.0.0-alpha.46"'.

To fix the issue

  1. run rm -rf node_modules/ to remove the node_modules folder
  2. open package.json and look for "@material-ui/lab": "^4.0.0-alpha.XX"
  3. modify the current version to "@material-ui/lab": "4.0.0-alpha.46"
  4. run npm install
  5. run npm start

Upvotes: 0

Dhika Putra Ardana
Dhika Putra Ardana

Reputation: 21

Its kind of bug on current tags so far, you could try to change your @material-ui/lab module's version

From:

"@material-ui/lab": "^4.0.0-alpha.49"

To:

"@material-ui/lab": "4.0.0-alpha.46"

Upvotes: 2

Related Questions