0

I am trying to include Semantic UI with ES6 imports inside my React component.

I am using Grunt with Babel + Browserify.

I have already installed Semantic UI via NPM.

Here is Gruntfile browserify config:

  grunt.initConfig({
    browserify: {
      dist: {
        options: {
          transform: [
            ['babelify', {
              presets: ['es2015', 'react']
            }]
          ],
          watch: true
        },
        files: {
          './dist/app.js': ['./src/js/app.js']
        }
      }
    },

I have created component like this:

import React from 'react'
import $ from 'jquery'
import dropdown from 'semantic-ui'

class Container extends React.Component {
  constructor() {
    super()
    this.state = {
      value: null
    }
  }

  componentDidMount() {
    $('.ui.selection.dropdown').dropdown({
      dataType: 'jsonp',
      apiSettings   : {
        onResponse: function(githubResponse) {
          var
            response = {
              results : []
            }
          ;
          // translate github api response to work with dropdown
          $.each(githubResponse.items, function(index, item) {
            response.results.push({
              name: item.name,
              value: item.id
            });
          });
          return response;
        },
        url: '//api.github.com/search/repositories?q={query}'
      },
      onChange: (value) => {
        this.setState({
            value
        });
      }
    });
  }

  componentDidUpdate() {
      $('.ui.dropdown').dropdown('refresh');
  }

  render() {
      return (
          <div>
              <div>
                  <h2>Dropdown</h2>
                  <div className="ui fluid multiple search selection dropdown">
                      <input type="hidden" name="repo-ids" />
                      <div className="default text">Select Repos</div>
                      <i className="dropdown icon"></i>
                      <div className="menu">
                      </div>
                  </div>
               </div>
              <div>
                  <div className="ui divider"></div>
                  <b>Selected value</b> {this.state.value}
              </div>
          </div>
      );
  }
};

export default Container

However, when I try to compile this code, I get:

Error: Cannot find module 'semantic-ui'

Any help ? Do I need to setup browserify somehow, to compile Semantic UI ?

Michał Lach
  • 1,209
  • 4
  • 17
  • 33
  • I haven't used semantic-ui. From their site I see that you might need to do this: import dropdown from 'semantic-ui-dropdown' Give it a go and let me know if it works! – Antonis Zisis Jun 24 '16 at 10:47
  • No luck with it, getting: `Error: Cannot find module 'semantic-ui-dropdown` – Michał Lach Jun 24 '16 at 11:03
  • Can you double check your package.json file that the 'semantic-ui' module is installed? Because your error suggests that the module is not there. Also use import { dropdown } from 'semantic-ui' as @Ashitaka suggested. – Antonis Zisis Jun 24 '16 at 11:10
  • Yes, I have installed semantic-ui via NPM it is present inside my package.json, also `import { dropdown } from 'semantic-ui' doesnt work either :/ – Michał Lach Jun 24 '16 at 11:13
  • Interesting... Could you also try import * from 'semantic-ui'. Just in case... – Antonis Zisis Jun 24 '16 at 11:18
  • Already tried that too, no luck – Michał Lach Jun 24 '16 at 11:22
  • Possible duplicate of [NodeJS - How to resolve "Cannot find module" error](http://stackoverflow.com/questions/9023672/nodejs-how-to-resolve-cannot-find-module-error) – Paul Sweatte Sep 06 '16 at 03:31

1 Answers1

2

Actually, there is a separate library of Semantic-UI for Reactjs. You need to install that and use it in your Reactjs application. To install just do npm install semantic-ui-react --save and you can find everything. Here is a link to its official site: semantic-ui-react

Arslan Tariq
  • 2,208
  • 2
  • 17
  • 42