Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

reactjs - React-share. Throws error TypeError: Super expression must either be null or a function, not undefined when trying to use it in 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

My code looks like this:

"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');

I have to use react-share's

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

Component to share the shareLink on facebook.

Here the full code.

/*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>;
            usageCon

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found my error!

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>` 

Instead, I should have initialized the require statement as

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

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

I hope this will save someones precious time. Cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...