材质UI输入自动完成功能无法在React.js中使用

时间:2020-04-14 12:40:13

标签: reactjs input material-ui

当我尝试完全从文档中导入自动完成时: https://material-ui.com/components/autocomplete/我收到以下错误消息:

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').

反应代码:

/* 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 }
];

MUI的当前版本:

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

2 个答案:

答案 0 :(得分:1)

到目前为止,这是当前标签上的一种错误,您可以尝试更改@ material-ui / lab模块的版本

发件人:

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

收件人:

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

答案 1 :(得分:0)

这是对@Dhika的上述回答的补充。

他的解决方案在我第一次尝试时不起作用,因为我没有注意到版本'"4.0.0-alpha.46"'前面没有插入符号。

要解决此问题

  1. 运行rm -rf node_modules/删除node_modules文件夹
  2. 打开package.json并查找"@material-ui/lab": "^4.0.0-alpha.XX"
  3. 将当前版本修改为"@material-ui/lab": "4.0.0-alpha.46"
  4. 运行npm install
  5. 运行npm start