如何使用React和Babel从React-Select CDN导入?

时间:2018-12-26 04:05:21

标签: javascript reactjs babeljs react-select

我已经按照React Website上的步骤创建了一个react应用程序,但是在使用React-Select Library(尤其是Select Component)方面遇到了问题。

我只能使用CDN文件加载依赖项,包括位于cdnjs上的React-Select CDN文件

https://cdnjs.cloudflare.com/ajax/libs/react-select/2.1.2/react-select.js

我的react-app出现以下错误:

ReferenceError: Select is not defined[Learn More]

请参见下面的脚本和here for my codepen

  <html> 
    <head>
      <meta charset="utf-8">
      <script src="https://unpkg.com/react@15.0.0/dist/react-with-addons.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.5/react-redux.min.js"></script>
      <script src="https://unpkg.com/react-dom@0.14.0/dist/react-dom.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-select/2.1.2/react-select.js"></script>
      <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
      <title>Using React Select CDN</title>
    </head>
    <body>
      <div id="root"></div>
      <script type="text/babel">


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

      class App extends React.Component {
        state = {
          selectedOption: null,
        }
        handleChange = (selectedOption) => {
          this.setState({ selectedOption });
          console.log(`Option selected:`, selectedOption);
        }
        render() {
          const { selectedOption } = this.state;

          return (
            <div>
            Test Text
            <Select
              value={selectedOption}
              onChange={this.handleChange}
              options={options}
            />
            </div>
          );
        }
      }

      ReactDOM.render(<App/>, document.querySelector("#root"))
      </script>
    </body>
  </html>

我还尝试了以下引发相同错误的操作

  <html> 
    <head>
      <meta charset="utf-8">
      <script src="https://unpkg.com/react@15.0.0/dist/react-with-addons.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.0.4/redux.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.5/react-redux.min.js"></script>
      <script src="https://unpkg.com/react-dom@0.14.0/dist/react-dom.min.js"></script>


      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-select/2.1.2/react-select.js"></script>
      <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-select/2.1.2/react-select.js"></script>

      <title>Using React Select CDN</title>
    </head>
    <body>
      <div id="root"></div>

      <script type="text/babel">

      require(['react-select'])
      import Select from 'react-select';
      const options = [
        { value: 'chocolate', label: 'Chocolate' },
        { value: 'strawberry', label: 'Strawberry' },
        { value: 'vanilla', label: 'Vanilla' }
      ];

      class App extends React.Component {
        state = {
          selectedOption: null,
        }
        handleChange = (selectedOption) => {
          this.setState({ selectedOption });
          console.log(`Option selected:`, selectedOption);
        }
        render() {
          const { selectedOption } = this.state;

          return (
            <div>
            Test Text
            <Select
              value={selectedOption}
              onChange={this.handleChange}
              options={options}
            />
            </div>
          );
        }
      }

      ReactDOM.render(<App/>, document.querySelector("#root"))
      </script>
    </body>
  </html>

如何使React-Select起作用?也许

1 个答案:

答案 0 :(得分:1)

它将查找您需要添加最新依赖项的最新react-select-

Codepan

<script src="https://unpkg.com/react@16.7.0/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/emotion@9.2.12/dist/emotion.umd.min.js"></script>
<script src="https://unpkg.com/react-dom@16.7.0/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/prop-types@15.5.10/prop-types.min.js"></script>
<script src="https://unpkg.com/react-input-autosize@2.2.1/dist/react-input-autosize.min.js"></script>
<script src="https://unpkg.com/react-select@2.1.2/dist/react-select.min.js"></script>

<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>