反应份额.引发错误TypeError:超级表达式必须为null或函数,在require中尝试使用超级表达式时必须未定义 [英] React-share. Throws error TypeError: Super expression must either be null or a function, not undefined when trying to use it in require

查看:283
本文介绍了反应份额.引发错误TypeError:超级表达式必须为null或函数,在require中尝试使用超级表达式时必须未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ReactJs的新手,正在尝试学习它.我安装了react-share软件包.由于我正在尝试编辑其他人的代码,因此我相信由于webpack而无法导入软件包.每次尝试导入软件包时,都会收到一条错误消息,提示导入应始终位于脚本的顶部.我尝试使用require并在控制台中收到此错误

I am new in ReactJs and trying to learn it. I installed a package of react-share. Since i am trying to edit someone else's code i am not able to import the package due to webpack I believe. Every time i try to import a package I receive an error saying the import should always be on top of the script. I tried using require and I get this error in Console

TypeError: Super expression must either be null or a function, not undefined

我的代码如下:

"use strict";

require('./../../../assets/styles/components/thread.less');


var reactShare = require('react-share');
var React = require('react');
var ReactDOM = require('react-dom');
var Fluxxor = require('fluxxor');
var _ = require("lodash");

var FluxMixin = Fluxxor.FluxMixin(React);
var StoreWatchMixin = Fluxxor.StoreWatchMixin;

var routerShape = require('react-router').routerShape;
var MicroAudioViews = require('./../../constants/MicroAudioViews');
var AudioModes = require("./../../constants/AudioModes");
var i18n = require("i18next-client");

//components
var AudioVisualizer = require('../elements/AudioVisualizer');
var ReviewOverlay = require('../elements/ReviewOverlay');
var ReviewShare = require('../elements/ReviewShare');
var Menu = require('../elements/Menu');

我必须使用react-share

<FacebookShareButton url={shareLink} quote={title} className="social-media-icon">
  <FacebookIcon size={32} round />
 </FacebookShareButton>` 

在Facebook上共享shareLink的组件.

Component to share the shareLink on facebook.

这里是完整代码.

/*import {
    FacebookShareButton,
    GooglePlusShareButton,
    TwitterShareButton,
    WhatsappShareButton,

    FacebookIcon,
    TwitterIcon,
    WhatsappIcon

} from 'react-share';*/

"use strict";

require('./../../../assets/styles/components/thread.less');


var reactShare = require('react-share');
var React = require('react');
var ReactDOM = require('react-dom');
var Fluxxor = require('fluxxor');
var _ = require("lodash");

var FluxMixin = Fluxxor.FluxMixin(React);
var StoreWatchMixin = Fluxxor.StoreWatchMixin;

var routerShape = require('react-router').routerShape;
var MicroAudioViews = require('./../../constants/MicroAudioViews');
var AudioModes = require("./../../constants/AudioModes");
var i18n = require("i18next-client");

//components
var AudioVisualizer = require('../elements/AudioVisualizer');
var ReviewOverlay = require('../elements/ReviewOverlay');
var ReviewShare = require('../elements/ReviewShare');
var Menu = require('../elements/Menu');

var Review = React.createClass({
    mixins:[    
        FluxMixin,
        StoreWatchMixin("ThreadStore", "RecordStore", "ReviewStore", "ApplicationStore", "SyncStore", "DemoStore", "ShareStore")
    ],

    contextTypes: {
        router: routerShape.isRequired
    },

    /* react interface*/
    getInitialState: function() {
        var selectedThreads = [];
        var shareType = 'thread';
        if(this.props.location.state && this.props.location.state.type == "thread") {
            selectedThreads.push(this.props.location.state.threadId);
        } else if(this.props.location.state && this.props.location.state.type == "share") {
            shareType = 'facebook';
        } else if (this.props.location.state && this.props.location.state.type == 'sharereply') {
            shareType = 'sharereply';
        }
        return {
            threadUserId: this.props.params.id,
            activeShareType: shareType,
            selectedThreads: selectedThreads
        };
    },

    getStateFromFlux: function() {
        var flux = this.getFlux();
        var recordStoreState = flux.store('RecordStore').getState();
        var threadStoreState = flux.store('ThreadStore').getState();
        var appStoreState = flux.store('ApplicationStore').getState();
        var reviewStoreState = flux.store('ReviewStore').getState();
        var shareStoreState = flux.store('ShareStore').getState();
        var demoState = flux.store('DemoStore').getState();

        var activeRecord = recordStoreState.activeRecord || null;
        var activeThread = threadStoreState.activeThread;
        var activeRecordUser = null;

        var authenticatedUser = appStoreState.demoMode? demoState.user : appStoreState.user;
        var state = {
            demoMode: appStoreState.demoMode,
            playing: recordStoreState.playing,
            recording: recordStoreState.recording,
            activeThread: activeThread,
            threads: threadStoreState.threads,
            authenticatedUser: authenticatedUser,
            activeRecord: activeRecord,
            activeShareUser: shareStoreState.user,
            shareId: shareStoreState.shareId
        };
        return state;

    },

    render: function() {
        var threadClass = "thread";
        var fbClass = "facebook";
        var explanationText, usageContent;
        var finishButtonClass = 'finish-button';
        if(this.state.activeShareType == "thread") {
            threadClass += ' active';
            explanationText = i18n.t('content:review.reviewDoneExpl', {
                count: this.state.selectedThreads.length,
                context: this.state.selectedThreads.length == 0 ? 'doselect' :  undefined
            });
            finishButtonClass += this.state.selectedThreads.length == 0 ? ' inactive' :  '';

            var threadCards = [];
            var self = this;

            _.each(this.state.threads, function(thread){
                var threadUser = thread.user;
                var threadUserPicture = threadUser.pictures[0].source;
                var userName = threadUser.firstName + ' ' + threadUser.lastName;
                var styleProps = {
                    backgroundImage :  threadUserPicture ? 'url(' + threadUserPicture + ')': 'none'
                };
                var cls = "thread card" + (self.state.selectedThreads.indexOf(thread.id) != -1? " selected" : "");
                    threadCards.push(<div key={thread.id} className={cls} onClick={self.onThreadCardSelected} data-thread-id={thread.id}>
                            <div className='pic' style={styleProps}></div>
                            <div className='name'>{userName}</div>
                            <div className='checked micro-audio-icon-check'></div>
                        </div>);
            });


            //if thread cards array is null then we are displaying the required text
            if(threadCards.length==0){
                var text= "Du hast noch keine Freunde in audiyoh hinzugefugt (gehe dafur zur Suche).";

            //displaying the content
            usageContent = (
                <div className="usage-target-container">
                    <p className="chat-text">{text} <br/>Uber <img className="share-icon" src={require('./../../../assets/images/share-active.png')} /> Teilen kannst du deine Aufnahme in aderen Kanale teilen.</p>
                </div>);

            //displaying the button
            var finishContainer = <div className="finish-container">
                                            <div    className={finishButtonClass} >Fertige</div>
                                            <div    className="finish-text"><p className="chat-underbtn-text">Mindestens <b> ein Gesprach <br/> wahlen,</b> dem die Aufnahme <br/> hinzugefugt werden soll</p></div>
                                        </div>;             
            }else{
                usageContent = (
                    <div className="usage-target-container">
                        {threadCards}
                    </div>);    
            }

        } else {
            fbClass += ' active';
            finishButtonClass += ' facebook';
            explanationText = i18n.t('content:review.facebookExplanation');
            //displaying the input box with the link and copy button
            console.log("THe shareStoreState is " + this.state.shareId);

            //the shareId is generate asynchroneously, so this.state.shareId can be null
            if(typeof this.state.shareId === "string") {
                //the link can be created like this:
                var shareLink = window.location.origin + '/shared/' + this.state.shareId;
            }

            var usageContent = (
                    <div className="usage-target-container">
                        <div className="socialLinkContainer">
                            <p> Link zum Teilen </p>
                            <input className="copylink" type="text" value={shareLink} id="shareLink" /><br/>
                            <input className="copybtn" type="button" onClick={this.copytoclipboard} value="Link kopieren" /> 
                        </div>
                    </div>);

            var finishContainer = <div className="finish-container">
                                    <div className="social-media">
                                        /*<a href="" onClick=""><img className="social-media-icon" src={require('./../../../assets/images/facebook.png')} /></a>*/
                                        <FacebookShareButton
                                            url={shareLink}
                                            quote={title}
                                            className="social-media-icon">
                                            <FacebookIcon
                                              size={32}
                                              round />
                                          </FacebookShareButton>

                                        <a href="" onClick=""><img className="social-media-icon" src={require('./../../../assets/images/whatsapp.png')} /></a>
                                        <a href="" onClick=""><img className="social-media-icon" src={require('./../../../assets/images/twitter.png')} /></a>
                                        <a href="" onClick=""><img className="social-media-icon" src={require('./../../../assets/images/instagram.png')} /></a>
                                    </div>

                                </div>;
        }
        var targetSwitchElements = [
            <div title={i18n.t('content:review.sharethread')} 
                key="thread" 
                className={threadClass} 
                onClick={this.activateThreadShareType}><span>audiyoh-chat</span></div>,<br/>,
            <div title={i18n.t('content:review.sharefb')} 
                key="facebook" 
                className={fbClass} 
                onClick={this.activateFBShareType}><span>Teilen</span></div>
        ];




        //we either want to save a profile record a share response, so we dont need the fb/thread  switch and thread cards
        if(this.props.location.state && ["profile", "sharereply"].indexOf(this.props.location.state.type) != -1) {
            var buttonText = i18n.t('content:review.profile');
            if(this.props.location.state.type == "sharereply"){
             buttonText = i18n.t('content:review.share', {name: this.state.activeShareUser.firstName});
            }
            targetSwitchElements = <div className="profile-record" onClick={this.onFinishRecord}>{buttonText}</div>;
            usageContent = null;
            finishContainer = null;
        }


        return (
            <div className="ma-reviewing">
                <div className="review-controls">
                <div className="row">
                    <div className="col-3">
                        <a title={i18n.t('content:review.delete')} className="delete" onClick={this.deleteRecording}></a>
                        <a title={i18n.t('content:review.redo')} className="record" onClick={this.onRecordButtonClick}></a>
                    </div>

                    <div className="col-6">
                        <div className="review-container">
                            <ReviewOverlay 
                                activeRecordUser={this.state.authenticatedUser} 
                                record={this.state.activeRecord} 
                            />
                        </div>
                    </div>
                    {finishContainer}
                    <div className="col-3">
                        <div className="target-switch">
                            <p>Weiter mit der Aufnahme</p>
                            {targetSwitchElements}
                        </div>
                    </div>
                </div>
                </div>
                <div className="upper" ref="upper">
                    <Menu location={this.props.location} />
                    <div className="menu">
                    </div>
                </div>
                <div className="upperguard" ref="upperguard"></div>
                <div className="lower" ref="lower">
                    <div className="sizing-wrapper">
                        {usageContent}
                    </div>
                </div>
            </div>
        );
    },

    deleteRecording: function(e) {
        e.preventDefault();
        if(this.props.location && this.props.location.state.userId) {
            this.context.router.push({
                pathname: "/thread/" + this.props.location.state.userId,
                state: this.props.location.state
            });
        } 
        else {
            this.context.router.push({
                pathname: "/profile",
                state: this.props.location.state
            });
        }
    },

    onRecordButtonClick: function(e) {
        e.preventDefault();
        this.context.router.push({
            pathname: "/record",
            state: this.props.location.state
        });
    },

    onThreadCardSelected: function(syntheticEvent, reactId, e) {
        var target = syntheticEvent.target.parentNode;
        var threadId = target.getAttribute("data-thread-id");
        var idx = this.state.selectedThreads.indexOf(threadId);

        if(idx != -1) {
            this.state.selectedThreads.splice(idx, 1);
            this.setState({
                selectedThreads: [].concat(this.state.selectedThreads)
            });
        }
        else {
            this.setState({
                selectedThreads: [threadId].concat(this.state.selectedThreads)
            });
        }
    },

    activateThreadShareType: function() {
        if(this.state.activeShareType == "thread") {
            return;
        }

        this.setState({
            activeShareType: 'thread'
        });
    },
    activateFBShareType: function() {
        if(this.state.activeShareType == "facebook") {
            return;
        } 

        this.setState({
            activeShareType: 'facebook'
        });

        //this will store the record and generate a shareId
        this.getFlux().actions.record.local.saveRecording({
            type: "share"
        });
        /*var state1 = this.context.router.push({
            pathname: '/review',
            state: {
                type: "profile",
                role: "main"
            }
        });*/

        //console.log("the state1    is " + state1);
        //this.getFlux().actions.record.local.saveRecording(state1);
    },

    copytoclipboard: function(){

          var copyText = document.getElementById("shareLink");
          copyText.select();
          document.execCommand("copy");
          console.log("Copied the text: " + copyText.value);

    },

    onFinishRecord: function(e) {
        if(e.target.classList.contains('inactive')) {
            return;
        } 

        if(this.props.location.state && ["profile", "sharereply"].indexOf(this.props.location.state.type) != -1) {
            console.log(this.props.location.state);
            this.getFlux().actions.record.local.saveRecording(this.props.location.state);
        }
        else if(this.state.activeShareType == "facebook") {
            this.getFlux().actions.record.local.saveRecording({
                type: "share"
            });
        }
        else {
            var data = {
                type: "thread",
                threadIds: this.state.selectedThreads
            };

            //we started recording from a thread, so we pass the userId to be able to return to this thread
            //after saving
            if(this.props.location.state && this.props.location.state.type == "thread") {
                data.userId = this.props.location.state.userId;
            }

            this.getFlux().actions.record.local.saveRecording(data);
        }
    }
});

module.exports = Review;

推荐答案

我发现了我的错误!

问题是我在变量reactShare中初始化了require('react-share'),并将组件用作

The problem was that I initialized the require('react-share') in a variable reactShare and was using the component as

<FacebookShareButton url={shareLink} quote={title} className="social-media-icon">
  <FacebookIcon size={32} round />
 </FacebookShareButton>` 

相反,我应该将require语句初始化为

Instead, I should have initialized the require statement as

var FacebookShareButton = require('react-share');

由于没有正确声明React在生我的气.

Because of not declaring it properly React was yelling on me.

我希望这可以节省别人的宝贵时间.干杯!

I hope this will save someones precious time. Cheers!

这篇关于反应份额.引发错误TypeError:超级表达式必须为null或函数,在require中尝试使用超级表达式时必须未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆