简体   繁体   中英

Error while using react-virtualized-select

Trying to use 'react-virtualized-select' as per https://github.com/bvaughn/react-virtualized-select

we are using require.js in our application imported after getting it from npm and didn't the import css ....as mentioned here https://github.com/bvaughn/react-virtualized-select#simple-example

在此处输入图片说明

Note : posting full code to make easy to understand context Please ignore any short coming's in rest of code as the error disappears if i remove the VirtualizedSelect tag

    var React = require('react');
var ReactDOM = require('react-dom');
var VirtualizedSelect = require('react-virtualized-select');

module.exports = React.createClass({
    componentName: 'Search',
    getDefaultProps() {
        return {};
    },
    getInitialState() {
        return {
            error: false,
            authenticated: false,
            visible: true,
            data: null,
            showAccountSelection: false,
            selected: null,
            search: '',
            optionPartnerList: []

        };
    },
    componentDidMount () {
        var qS = queryString.parse(location.search);
        if (qS && qS.search) {
            this.setState({search: qS.search, visible: true});
        }

        this._unsubscribe = AppStore.listen(Utils.createRefluxComponentDispatcher(this));
    },
    componentDidUpdate: function () {
        if (this.refs.inputSearch) {
            this.refs.inputSearch.focus();
            this.refs.inputSearch.select()
        }
    },

    componentWillUnmount () {
        this._unsubscribe();
    },

render: function () {
        var self= this;
        if (!this.state.authenticated) {
            return null;
        }
        let componentDisabled = this.state.async ? true : false;
        let buttonIcon;
        let closeButton = AppStore.selectedAccount ?
            <a href="#" className="closebtn" onClick={this.closeNav}>&times;</a> :
            (<a key="-1" className="logout-link dropdown-item t-font" onClick={AppActions.logout} href="#">
                <i className="fa fa-sign-out m-r-10"></i>Logout</a>);

        let overlayStyle = {width: this.state.visible ? '100%' : '0px', display: this.state.visible ? 'block' : 'none', 'overflowX': 'hidden', 'overflowY': this.state.overlayOverflowY};
        if (this.state.visible) {
            AppActions.hideBodyScrollBar();
        } else {
            AppActions.showBodyScrollBar();
        }

        if (!this.state.async) {
            buttonIcon = <i className="fa fa-search"></i>
        } else {
            buttonIcon = <i className="fa fa-cog fix fa-spin"></i>
        }
        //TODO: refactor this make it css driven using flex box
        let style = {top: '37%'};
        if (this.state.data && Object.keys(this.state.data).length > 1) {
            style = {top: '10%'};
        }
        return (

            <div style={{float:'left'}} className="search-div">
                {this.searchIcon()}
                <div className="overlay" style={overlayStyle}>
                    {closeButton}
                    <div className="global-search center-content-wrapper" style={style}>
                        <form id="searchFormComponent" ref={function(){$('#searchFormComponent').show('fast')}}
                              className="global-search" onSubmit={this.handleClick} style={{height: '500px'}}>
                            {this.errorRender()}
                            <div className="f-row f-center">
                                <input id="searchbox" type="text" ref="inputSearch" className="form-control f-9 searchbox"
                                       placeholder="SEARCH FOR ACCOUNT" required
                                       style={{marginBottom:'1px'}} disabled={componentDisabled}
                                       defaultValue={this.state.search}>
                                </input>
                                <VirtualizedSelect
                                    options={self.optionPartnerList}
                                    onChange={(selectValue) => this.setState({ selectValue })}
                                    value={this.state.selectValue}
                                />
                                <button id="searchbutton" className="btn btn-lg btn-primary btn-block t-font f-1"
                                        disabled={componentDisabled}
                                        onClick={this.handleClick}
                                        style={{ paddingLeft: '4px',paddingRight: '4px', fontSize:'1.1em',marginLeft:'4px'}}>
                                    {buttonIcon}
                                </button>
                                <SearchHint ref={"searchHint"} toggleOverlayStyle={this.toggleOverlayStyle}/>
                            </div>
                            <SearchAccountSelector data={this.state.search == ''? {}: this.state.data}/>
                        </form>
                    </div>
                </div>
            </div>
        );
    }
}); 

getting this exception

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of `exports`.     at invariant (bundle.c81618e7.js:5333)     at instantiateReactComponent (bundle.c81618e7.js:27936)     at Object.updateChildren (bundle.c81618e7.js:17273)     at ReactDOMComponent._reconcilerUpdateChildren (bundle.c81618e7.js:23289)     at ReactDOMComponent._updateChildren (bundle.c81618e7.js:23393)     at ReactDOMComponent.updateChildren (bundle.c81618e7.js:23380)     at ReactDOMComponent._updateDOMChildren (bundle.c81618e7.js:19340)     at ReactDOMComponent.updateComponent (bundle.c81618e7.js:19154)     at ReactDOMComponent.receiveComponent (bundle.c81618e7.js:19116)     at Object.receiveComponent (bundle.c81618e7.js:24009)

Not able to find the mistake i made any help in this regard will be greatly appreciated please find my package.json entries

"react": "^15.6.2",
"react-dimensions": "^1.3.0",
"react-dom": "^15.6.2",
"react-virtualized-select": "3.1.0",

The problem is with the line:

var VirtualizedSelect = require('react-virtualized-select');

Which needs to be changed to:

var VirtualizedSelect = require('react-virtualized-select').default;

The error message is because what's being imported isn't a React component, and seemingly has to do with the way that the export default in React Virtualized Select's source has been traspiled into ES5.

I think you forgot to add the render method for the class component.

checkout the code below

  var React = require('react'); var ReactDOM = require('react-dom'); var VirtualizedSelect = require('react-virtualized-select'); module.exports = React.createClass({ componentName: 'Search', getDefaultProps() { return {}; }, getInitialState() { return { error: false, authenticated: false, visible: true, data: null, showAccountSelection: false, selected: null, search: '', optionPartnerList: [] }; }, componentDidMount () { var qS = queryString.parse(location.search); if (qS && qS.search) { this.setState({search: qS.search, visible: true}); } this._unsubscribe = AppStore.listen(Utils.createRefluxComponentDispatcher(this)); }, componentDidUpdate: function () { if (this.refs.inputSearch) { this.refs.inputSearch.focus(); this.refs.inputSearch.select() } }, componentWillUnmount () { this._unsubscribe(); } render() { return ( <div style={{float:'left'}} className="search-div"> {this.searchIcon()} <div className="overlay" style={overlayStyle}> {closeButton} <div className="global-search center-content-wrapper" style={style}> <form id="searchFormComponent" ref={function(){$('#searchFormComponent').show('fast')}} className="global-search" onSubmit={this.handleClick} style={{height: '500px'}}> {this.errorRender()} <div className="f-row f-center"> <input id="searchbox" type="text" ref="inputSearch" className="form-control f-9 searchbox" placeholder="SEARCH FOR ACCOUNT" required style={{marginBottom:'1px'}} disabled={componentDisabled} defaultValue={this.state.search}> </input> <VirtualizedSelect options={self.optionPartnerList} onChange={(selectValue) => this.setState({ selectValue })} value={this.state.selectValue} /> <button id="searchbutton" className="btn btn-lg btn-primary btn-block t-font f-1" disabled={componentDisabled} onClick={this.handleClick} style={{ paddingLeft: '4px',paddingRight: '4px', fontSize:'1.1em',marginLeft:'4px'}}> {buttonIcon} </button> </div> </form> </div> </div> </div> ); } } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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