在我自己的示例中,我努力用材料ui实现一个版本的react-select。文档中给出的示例非常复杂。 https://material-ui.com/components/autocomplete/
当我尝试复制代码时,出现以下错误
ERROR in [at-loader] ./src/public/components/select.tsx:13:37
TS2307: Cannot find module 'react-select/lib/components/containers'.
对于以下依赖性。
import { ValueContainerProps } from "react-select/lib/components/containers";
import { ControlProps } from "react-select/lib/components/Control";
import { MenuProps, NoticeProps } from "react-select/lib/components/Menu";
import { MultiValueProps } from "react-select/lib/components/MultiValue";
import { OptionProps } from "react-select/lib/components/Option";
import { PlaceholderProps } from "react-select/lib/components/Placeholder";
import { SingleValueProps } from "react-select/lib/components/SingleValue";
import { ValueType } from "react-select/lib/types";
我已经安装了@ types / react-select和react-select。
答案 0 :(得分:1)
您可以尝试以类似方式导入(没有实际路径)-
import { ValueContainerProps } from "react-select";
react-select内也没有“ lib”文件夹。您确定要给出正确的路径吗?
答案 1 :(得分:1)
您在帖子中提供的链接,即-https://material-ui.com/components/autocomplete/
此链接不包含任何导入示例
import { ValueContainerProps } from "react-select/lib/components/containers";
import { ControlProps } from "react-select/lib/components/Control";
import { MenuProps, NoticeProps } from "react-select/lib/components/Menu";
import { MultiValueProps } from "react-select/lib/components/MultiValue";
import { OptionProps } from "react-select/lib/components/Option";
import { PlaceholderProps } from "react-select/lib/components/Placeholder";
import { SingleValueProps } from "react-select/lib/components/SingleValue";
import { ValueType } from "react-select/lib/types";
使您的react-select
工作的简单步骤,
使用安装react-select
,
yarn add react-select / npm install react-select --save
在您的组件中导入默认的导出和渲染:
import Select from 'react-select'
用法
<Select options={options} />
您需要在此处传递options
,除了下拉菜单之外,其他什么都没有。
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
]