【问题标题】:Error while using react-virtualized-select使用 react-virtualized-select 时出错
【发布时间】:2018-04-16 16:21:51
【问题描述】:

尝试按照https://github.com/bvaughn/react-virtualized-select 使用“react-virtualized-select”

我们在我们的应用程序中使用了 require.js 在从 npm 获取它后导入,并且没有导入 css ....as 提到这里 https://github.com/bvaughn/react-virtualized-select#simple-example

注意:发布完整代码以使上下文易于理解 请忽略其余代码中的任何缺点,因为如果我删除 VirtualizedSelect 标记,错误就会消失

    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>
        );
    }
}); 

得到这个异常

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)

找不到我在这方面提供任何帮助的错误将不胜感激,请找到我的 package.json 条目

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

【问题讨论】:

  • 通常当您忘记导出您创建的某些组件时会发生这种情况。你已经检查过了吗?另外,如果您能提供一些有用的代码。
  • 我可以看看您是如何在组件中使用该元素的吗?显示更多代码
  • @BrunnoVodolaMartins 感谢您的回复!根据 Git 中的示例代码,我们需要像我提到的那样导入 css,但我没有这样做......你认为这是导致问题的原因吗?
  • @user2359997 我不认为缺少的 CSS 文件是这里的问题。那只会让你的组件看起来没有样式。您能否发布围绕您的VirtualizedSelect 的组件的完整代码?
  • 试试var VirtualizedSelect = require('react-virtualized-select').default;

标签: javascript node.js reactjs react-redux


【解决方案1】:

问题出在这行:

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

需要改成:

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

错误消息是因为导入的不是 React 组件,并且似乎与 React Virtualized Select 源代码中的 export default 被转换为 ES5 的方式有关。

【讨论】:

  • 关于如何添加问题中提到的 css 的任何想法
  • 你是如何捆绑你的 JS 源代码的?文档中的方法假设您使用的是特定的 webpack 配置(与 Create React App 使用类似的设置)。
  • 如果一切都失败了,您可以像往常一样直接在 index.html 文件中添加 CSS &lt;link&gt; 标签
【解决方案2】:

我想你忘记为类组件添加 render 方法了。

查看下面的代码

    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>
        );
   }
    }
}); 

【讨论】:

  • 我之前也渲染过
  • 关于如何添加 css 的任何想法
  • 你的自定义css?
猜你喜欢
  • 2017-10-21
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2017-01-13
  • 2017-08-14
  • 2018-05-02
  • 2021-01-01
相关资源
最近更新 更多