2016-05-05 15 views
8

私はreact、reduxおよびreact-reduxを使用してストアでコンポーネントを実行しました。 コードのバンドルはwebpackで行います(下記のコードを確認してください)react、redux、およびreact-reduxで完了したコンポーネントを使用してエラーを表示し、反応アプリケーションでwebpackを使用して構築します。

私はwebpackビルドコンポーネントを別の反応プロジェクトの中で使いたいと思った時、次の問題に直面しました。

  • Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).

  • Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

それから私は、上に行って、コードに次の変更を行い、以前の私は、接続bindActionCreators

については、以下のような分割代入していました

import {connect} from 'react-redux'; 
 
import {bindActionCreators} from 'redux'; 
 
import actions from '../app/redux/actions';

は、その後、私は周りの

import React from 'react'; 
 

 
import connect from 'react-redux'; 
 
import bindActionCreators from 'redux'; 
 
import actions from '../app/redux/actions'; 
 

 
import postal from 'postal'; 
 

 
const channel = postal.channel("msplayer"); 
 

 
class Player extends React.Component {

を接続し、bindActionCreators中括弧を除去することで、以下のようにそれを変更しかし、私はこれを推測するように、その後、私は、以下のエラーに直面していますES6からES5に移行するバーベルとは関係がありますが、これを解決するためにどのような手順を実行するかわからない場合は、この解決策を得るための答えまたはいくつかのポインタを入手したいですか?

  • Uncaught TypeError: (0 , _reactRedux2.default) is not a function

  • Uncaught TypeError: Cannot read property 'PlayerWrapper' of undefined

コンポーネントコード

import React from 'react'; 
 

 
import {connect} from 'react-redux'; 
 
import {bindActionCreators} from 'redux'; 
 
import actions from '../app/redux/actions'; 
 

 
import postal from 'postal'; 
 

 
const channel = postal.channel("msplayer"); 
 

 
class Player extends React.Component { 
 

 

 
    constructor() { 
 
     super(); 
 
     this.state = { 
 
      userData: {}, 
 
      uiStates: { 
 
       panelClosed: true, 
 
       submissionSelected: false 
 
      }, 
 
      selectedSubmission: {} 
 
     }; 
 
     this.subSelectChannel = null; 
 
     this.tabSelectChannel = null; 
 
    } 
 

 

 
    componentWillMount() { 
 
     require('!style!css!../app/styles/player.css'); 
 
    } 
 

 
    componentDidMount() { 
 
     var _that = this; 
 
     var _msData = { 
 
      piToken: this.props.piToken, 
 
      sectionId: this.props.sectionId, 
 
      assignmentId: this.props.assignmentId, 
 
      userId: this.props.userId 
 
     }; 
 

 
     this.props.actions.getAssignmentData(msData); 
 
     this.props.actions.getPeerSubmissionData(msData); 
 

 
     this.subSelectChannel = channel.subscribe("submission.selected", function (data, envelope) { 
 
      _that.setState({ 
 
        uiStates: Object.assign({}, _that.state.uiStates, { 
 
         "submissionSelected": true 
 
        }) 
 
       } 
 
      ); 
 
      _that.setState({ 
 
        selectedSubmission: data.submission 
 
       } 
 
      ); 
 
     }); 
 

 
     this.tabSelectChannel = channel.subscribe("tab.selected", function (data, envelope) { 
 
      if (data.submitted) { 
 
       _that.showSubmissionDetailPanel(data.data); 
 
      } else { 
 
       _that.hideSubmissionDetailPanel() 
 
      } 
 
     }); 
 
    } 
 

 
    closePanel() { 
 

 
     postal.publish({ 
 
      channel: "notifier", 
 
      topic: "notifier.notify", 
 
      data: { 
 
       type: "warning", 
 
       message: "warning message" 
 
      } 
 
     }); 
 

 

 
     if (this.state.uiStates.panelClosed) { 
 

 
      this.setState({ 
 
        uiStates: Object.assign({}, this.state.uiStates, { 
 
         "panelClosed": false 
 
        }) 
 
       } 
 
      ); 
 
     } else { 
 

 

 
      postal.publish({ 
 
       channel: "msplayer", 
 
       topic: "close.selected", 
 
       data: {} 
 
      }); 
 

 
      this.setState({ 
 
        uiStates: Object.assign({}, this.state.uiStates, { 
 
         "panelClosed": true, 
 
         "submissionSelected": false 
 
        }) 
 
       } 
 
      ); 
 
     } 
 
    } 
 

 

 
    hideSubmissionDetailPanel() { 
 
     console.log("inside hide submission panel"); 
 
     this.setState({ 
 
       uiStates: Object.assign({}, this.state.uiStates, { 
 
        "submissionSelected": false 
 
       }) 
 
      } 
 
     ); 
 
    }; 
 

 
    showSubmissionDetailPanel(data) { 
 
     console.log("inside show submission panel"); 
 
     this.setState({ 
 
       uiStates: Object.assign({}, this.state.uiStates, { 
 
        "submissionSelected": true 
 
       }) 
 
      } 
 
     ); 
 

 
     this.setState({ 
 
      selectedSubmission: data 
 
     }); 
 
    }; 
 

 

 
    loadUserAssignmentData(submission) { 
 

 
     this.setState({ 
 
       uiStates: Object.assign({}, this.state.uiStates, { 
 
        "submissionSelected": true 
 
       }) 
 
      } 
 
     ); 
 

 
     postal.publish({ 
 
      channel: "msplayer", 
 
      topic: "submission.selected", 
 
      data: { 
 
       submission: submission 
 
      } 
 
     }); 
 

 
    } 
 

 
    componentWillUnmount() { 
 
     postal.unsubscribe(this.subSelectChannel); 
 
     postal.unsubscribe(this.tabSelectChannel); 
 
    } 
 

 
    render() { 
 

 
     var _that = this; 
 
     var _submittedKey = 0; 
 
     var _unsubmittedKey = 0; 
 
     return (
 
      <div className="player-container col-sm-12"> 
 
       <div className="row"> 
 
       </div> 
 
       <div className="row"> 
 
        <div className={_that.state.uiStates.panelClosed?"col-sm-12":"col-sm-8"}> 
 
         <div className="top-actions-panel"> 
 
          <div className="pull-right"> 
 

 
          </div> 
 

 

 
         </div> 
 
         <div className="common-view"> 
 
          <div className="breadcrumb-panel"> 
 
           <ol className="breadcrumb arrow-left"> 
 
            <li><a href="#">Communication 220</a></li> 
 
            <li className="active">TED Topics for an Informative Speech</li> 
 
           </ol> 
 
          </div> 
 
          <div className="description-panel"> 
 
           <p className="title"> 
 
            <b>Title</b>: 
 
            <span>{_that.props.assignment.title}</span> 
 
           </p> 
 
           <p className="dueDates font-light"> 
 
            <b>Due </b>:<span>{_that.props.assignment.startDate}</span> 
 
            <b> - </b><span>{_that.props.assignment.endDate}</span> 
 
           </p> 
 
           <p> 
 
            <b>Learning Objective: </b> 
 
            <span>{_that.props.assignment.learningObjective}</span> 
 
           </p> 
 
           <p> 
 
            <b>Description: </b> 
 
            <span> 
 
             {_that.props.assignment.description} 
 
            </span> 
 
           </p> 
 
          </div> 
 

 
          <div 
 
           className={_that.state.uiStates.submissionSelected?"row submission-info col-sm-12":"hidden"}> 
 
           <div> 
 
            <span className="student-avatar"> 
 
             <img 
 
              src={(_that.state.selectedSubmission && _that.state.selectedSubmission.userDetails && _that.state.selectedSubmission.userDetails.avatar && _that.state.selectedSubmission.userDetails.avatar!=="")?_that.state.selectedSubmission.userDetails.avatar:"../app/images/avatar.svg"}/> 
 
            </span> 
 

 
            <p> 
 
             <b> <span 
 
              className="font-light mediaTile"><strong>{(_that.state.selectedSubmission.title && _that.state.selectedSubmission.title !== null && _that.state.selectedSubmission.title !== "") ? _that.state.selectedSubmission.title : "."}</strong></span> 
 
             </b> 
 
            </p> 
 
            <br/> 
 
            <p> 
 
             <span 
 
              className="font-light ">{(_that.state.selectedSubmission.description && _that.state.selectedSubmission.description !== null && _that.state.selectedSubmission.description !== "") ? _that.state.selectedSubmission.description : "."}</span> 
 
            </p> 
 
           </div> 
 
          </div> 
 

 
          <div className="common-functionality-panel col-sm-12"> 
 
          </div> 
 
         </div> 
 
        </div> 
 
        <div 
 
         className={_that.state.uiStates.panelClosed?"hidden":"col-sm-4 no-padding peer-review-panel"}> 
 

 
         <div className="review-section"> 
 
          <button className="btn btn-link pull-left close-panel" 
 
            onClick={_that.closePanel.bind(_that)}> 
 
           <span className="reader-only">Close Student Submission Panel</span> 
 
           <i className="fa fa-times"></i> 
 
          </button> 
 

 
          <div className="submission-tabs"> 
 
          </div> 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </div> 
 
     ) 
 
    } 
 

 
} 
 

 
function mapStateToProps(state) { 
 
    return state 
 
} 
 

 
function mapDispatchToProps(dispatch) { 
 
    return { 
 
     actions: bindActionCreators(actions, dispatch) 
 
    } 
 
} 
 

 
export default connect(mapStateToProps, mapDispatchToProps)(Player)

コンポーネントラッパーコード

import React from 'react'; 
 
import Player from './app'; 
 

 
import bb from './redux/store' 
 
import Provider from 'react-redux'; 
 

 
class PlayerWrapper extends React.Component { 
 

 

 
    constructor(props) { 
 
     super(props); 
 
    } 
 

 
    render() { 
 
     return (
 
      <Provider store={bb.store}><Player piToken={this.props.piToken} sectionId={this.props.sectionId} 
 
               assignmentId={this.props.assignmentId} 
 
               userId={this.props.userId}/></Provider> 
 
     ) 
 
    } 
 
} 
 

 
export default PlayerWrapper;

WebPACKのエラーがreactDOM.renderまでお電話(WHIであることを示し、ファイル

var webpack = require('webpack'); 
 

 
module.exports = { 
 
    devtool: 'inline-source-map', 
 
    entry: [ 
 
     'webpack-hot-middleware/client', 
 
     './app/PlayerWrapper.js' 
 
    ], 
 
    output: { 
 
     path: require("path").resolve("./dist/app"), 
 
     filename: 'index.js', 
 
     publicPath: '/' 
 
    }, 
 
    plugins: [ 
 
     new webpack.optimize.OccurrenceOrderPlugin(), 
 
     new webpack.HotModuleReplacementPlugin(), 
 
     new webpack.NoErrorsPlugin() 
 
    ], 
 
    module: { 
 
     loaders: [{ 
 
      test: /\.js?$/, 
 
      exclude: /node_modules/, 
 
      loader: 'babel-loader', 
 
      query: { 
 
       presets: ['react', 'es2015'] 
 
      } 
 
     }, 
 
     { test: /\.css$/, loader: ["css-loader","style-loader"] }, 
 
     { test: /\.scss$/, loader: "sass-loader" }, 
 
     { test: /\.(ttf|eot|svg|eot|woff|otf|png|gif)(\?v)*/, loader: "file-loader?name=fonts/[name].[ext]" } 
 
     ] 
 
    } 
 
};

+1

ここで問題は何ですか? 'connect'と' bindActionCreators'は名前付きのインポートなので、中括弧を使うべきです。 –

+0

私はそれが問題を解決すると思ったことを投稿したが、後にあなたが言及したものを見つけた。今私は中括弧を使用している、私は最初のエラーを取得しています。問題を解決する方法が不思議で、最初にエラーメッセージが表示されるのはなぜですか? –

+0

最終的なコードを投稿してください。 –

答えて

0

を構築あなたは決して表示されません)、コンポーネントインスタンスの代わりに関数またはクラス名だけを渡しました。例えば

、以下の例が間違っている:

ReactDOM.render(MyComponentの、ドキュメント。getElementById( 'MyComponent')));

して修正は、角括弧の中にラップして、コンポーネントインスタンスに最初のパラメータをオンにすることである。

ReactDOM.render(< MyComponentの/ >、のdocument.getElementById( 'MyComponentの'));

13

ここで説明した2番目のエラーをデバッグするのにちょっと時間を費やしましたが、その過程でES6のインポート構文について少し学びました。

ライン:

import connect from 'react-redux';

反応-Reduxのライブラリからデフォルト輸出をインポートします。これは、エラーの発生源である:それを変更

Uncaught TypeError: (0 , _reactRedux2.default) is not a function

import { connect } from 'react-redux';

は、特定のケースでは、あなたが望むものであるという名前接続反応-Reduxのライブラリ内からオブジェクトをインポートします。 注意私が最初のようなエラーもあった中括弧

MDNのドキュメントをチェックアウトhere

:私は上記のように正確に私の定義されたコンポーネントをインポートしていないとき

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).

を。

+0

も魅力的です。ありがとう。 –

+0

'import {Class} ...'は 'import Class ...'と同じではありません - bravo!私は知らなかったし、誰かが私に言わなければそれを理解できなかっただろう! – pstanton

0

エラーがどこかにあなたが実際のコンポーネント(または文字列)ではない何かをレンダリングしようとしていることを意味

以前に、彼らはそれぞれのパッケージのデフォルトのエクスポートではありませんので、あなたが接続してbindActionCreators destructureする必要が述べたように。

小道具をレンダリングしようとしているときに、レンダリングするものは何もありません(例:ヌルまたは未定義)が、ReactDOM.renderという場所にコードを掲載していないため確実ではありません。