在React-Select和Typescript中创建可失败并显示错误

时间:2019-06-27 02:59:03

标签: reactjs typescript typescript-typings react-select

我正在创建一个React应用并使用react-select。我正在尝试使用Creatable部分,但是在导入它时出现错误。

我同时安装了react-select@types/react-select

npm install --save react-select
npm install --save-dev @types/react-select

当我像文档所说的那样导入

import Creatable from 'react-select/creatable';

我收到此类型错误

Could not find a declaration file for module 'react-select/creatable'. '/path/to/app/node_modules/react-select/creatable/dist/react-select.cjs.js' implicitly has an 'any' type.ts(7016)

我甚至尝试过这样导入

import { Creatable } from 'react-select';

但是我遇到了这个错误

Attempted import error: 'Creatable' is not exported from 'react-select'.

1 个答案:

答案 0 :(得分:0)

尝试一下

import React, { Component } from 'react';

import CreatableSelect from 'react-select/creatable';

export default class App extends Component {

  options = [
    { value: 'chocolate', label: 'Chocolate' },
    { value: 'strawberry', label: 'Strawberry' },
    { value: 'vanilla', label: 'Vanilla' },
  ];

  render() {

    return(
      <CreatableSelect
        isClearable
        options={this.options}
      />
    )
  }
}