How to import from React-Select CDN with React and Babel?

Tags: , , ,



I’ve created a react application following the steps on the React Website, but I’m having issues utilizing the React-Select Library (particularly the Select Component).

I can only use cdn files to load dependencies, including the React-Select cdn file located on cdnjs

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

I’m getting the following error with my react-app:

ReferenceError: Select is not defined[Learn More]

See below for my script and 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>

I’ve also tried the following which throws the same error

  <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>

How can I get React-Select to work? Perhaps

Answer

It looks for the latest react-select you need to add the latest dependencies –

Codepen

<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>


Source: stackoverflow