简体   繁体   English

在react组件中调用$()。dropdown

[英]Call $().dropdown inside react component

I'm using react version of Semantic-UI in a project and i need to create a custom dropdown component which has slightly different inner html structure than semantic-ui dropdown. 我在一个项目中使用Semantic-UI的 react版本,我需要创建一个自定义的dropdown组件,该组件的内部html结构与语义UI的dropdown略有不同 So i decided to define a new class to return my desired html structure like this: 因此,我决定定义一个新类,以返回所需的html结构,如下所示:

import React from 'react';
import {Icon} from 'semantic-ui-react';

class CustomDropdown extends React.Component {

    componentDidMount() {
        console.log(!!window.jQuery); // outputs 'true', which means jquery is defined 
        $('.ui.dropdown').dropdown();
    }

    render() {
        return(
            <div className="ui fluid search selection dropdown">
                <input name="states" type="hidden"/>
                <span className="highlight"></span>
                <span className="bar"></span>
                <label>Some text</label>
                <Icon name="dropdown"/>
                <div className="default text">States</div>
                <div className="menu">
                    <div className="item" data-value="AL">Alabama</div>
                    <div className="item" data-value="AK">Alaska</div>
                    <div className="item" data-value="AZ">Arizona</div>
                    <div className="item" data-value="AR">Arkansas</div>
                </div>
            </div>
        );
    }
}

export default CustomDropdown;

Jquery is loaded using webpack ProvidePlugin : jQuery是使用的WebPack加载ProvidePlugin

new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery'
}),

but when i use this component in my codes, the line $('.ui.dropdown').dropdown() gives error : 但是当我在代码中使用此组件时,行$('.ui.dropdown').dropdown()给出错误:

Uncaught TypeError: $(...).dropdown is not a function

So how can i use .dropdown() function inside my react component? 那么如何在我的react组件中使用.dropdown()函数呢?

@MehranTorki you will need to import dropdown function semantic. @MehranTorki您将需要导入下拉功能语义。

and you will need to do something like this 你将需要做这样的事情

import React from 'react'
import { Dropdown } from 'semantic-ui-react'

// TODO: This is missing functionality for sub-menu here from SUI core examples.
// The "Publish To Web" item should contain a sub-menu.

const DropdownExampleDropdown = () => (
  <Dropdown text='File'>
    <Dropdown.Menu> // -------------------------------
      <Dropdown.Item text='New' />
      <Dropdown.Item text='Open...' description='ctrl + o' />
      <Dropdown.Item text='Save as...' description='ctrl + s' />
      <Dropdown.Item text='Rename' description='ctrl + r' />
      <Dropdown.Item text='Make a copy' />
      <Dropdown.Item icon='folder' text='Move to folder' />
      <Dropdown.Item icon='trash' text='Move to trash' />
      <Dropdown.Divider />
      <Dropdown.Item text='Download As...' />
      <Dropdown.Item text='Publish To Web' />
      <Dropdown.Item text='E-mail Collaborators' />
    </Dropdown.Menu>
  </Dropdown>
)

to see what this code shows look at this url 看看这段代码显示了什么,看看这个URL

https://react.semantic-ui.com/maximize/dropdown-example-dropdown https://react.semantic-ui.com/maximize/dropdown-example-dropdown

Please look at the code samples over here for more examples 请查看此处的代码示例以获取更多示例

https://react.semantic-ui.com/modules/dropdown#dropdown-example-dropdown https://react.semantic-ui.com/modules/dropdown#dropdown-example-dropdown

I have copied the whole example but you just need to use the code which has the commented //------ line. 我已经复制了整个示例,但您只需要使用带有注释的// ------行的代码。 And import like the DropDown like I have imported to get things working 像我导入的DropDown一样导入以使工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM