0

I can't seem to make semantic ui work inside React component. JQuery is working properly. But semantic accordion doesn't respond to click event.

import React, { Component } from 'react'
import 'semantic-ui/dist/semantic.min.css'

import $ from 'jquery'
import jQuery  from 'jquery'
window.jQuery = jQuery
import 'semantic-ui/dist/semantic.min.js'

export default class App extends Component{
  render(){
    return(
      <div>
        <h1>Hello, React!</h1>
          <div className="ui slider checkbox">
            <input type="checkbox" name="newsletter" />
            <label>Accept terms and conditions</label>
          </div>
          <div className="ui styled accordion">
  <div className="active title">
    <i className="dropdown icon"></i>
    What is a dog?
  </div>
  <div className="active content">
    <p>A dog is a type of domesticated animal. Known for its loyalty and faithfulness, it can be found as a welcome guest in many households across the world.</p>
  </div>
  <div className="title">
    <i className="dropdown icon"></i>
    What kinds of dogs are there?
  </div>
  <div className="content">
    <p>There are many breeds of dogs. Each breed varies in size and temperament. Owners often select a breed of dog that they find to be compatible with their own lifestyle and desires from a companion.</p>
  </div>
  <div className="title">
    <i className="dropdown icon"></i>
    How do you acquire a dog?
  </div>
  <div className="content">
    <p>Three common ways for a prospective owner to acquire a dog is from pet shops, private owners, or shelters.</p>
    <p>A pet shop may be the most convenient way to buy a dog. Buying a dog from a private owner allows you to assess the pedigree and upbringing of your dog before choosing to take it home. Lastly, finding your dog from a shelter, helps give a good home to a dog who may not find one so readily.</p>
  </div>
</div>

      </div>
    )
  }
}
2
  • Is semantic-ui a node module? Or a file? If it is a file you need to make you import look like: import './path/to/file/filename'; Commented Jun 30, 2016 at 8:44
  • @DavinTryon It's a node module.. The CSS and JS files load perfectly, but for unknown reason js file cannot find JQuery. It says "uncaught reference: jquery" Commented Jun 30, 2016 at 8:46

3 Answers 3

6

Author of Semantic-UI-React here. Don't use jQuery in any React app. React maintains a virtual representation of the DOM that must be kept in perfect sync with the real DOM. In a React app, only React should manipulate the DOM. JQuery is a DOM manipulation Library. Using jQuery in a React app will break React.

This is why I wrote Semantic-UI-React: http://react.semantic-ui.com/introduction#jquery-free

https://github.com/Semantic-Org/Semantic-UI-React

Sign up to request clarification or add additional context in comments.

Comments

4

One way to include semantic-ui correctly in your project is

1º Install semantic-ui from npm

npm install semantic-ui-css --save

2º Import it after jquery. You have to use require, import would throw "uncaught reference: jquery"

window.$ = window.jQuery = require('jquery')
require('semantic-ui-css/semantic')

3º In your example, initialize the accordion after the component has been mounted

componentDidMount(){
    $('.ui.accordion') // You can use a ref here
      .accordion()
    ;
}

Your example working

4 Comments

See this comment stackoverflow.com/a/41913908/8997882. The author of Semantic-UI-React says not to use jQuery with it.
@MBak when this answer was written, Semantic-UI-React didn't exist & it was not possible to use semantic ui without jQuery.
Interesting. I'm pretty new to React so I did not know that.
I'm actually still struggling to get my Semantic UI React dropdown to work. Looks like only the 'simple' (non JS) version works. Do you know how to resolve this? I feel like I need to initialize it, but then the above answer says not to use jQuery.
0

I've just created a React Boiler Plate with Semantic Ui React. I put some intructions in README.md. Hope this will help https://github.com/kennethmervin01/react-with-semantic

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.