未捕获的ReferenceError:未定义createReactClass

时间:2017-11-01 18:48:09

标签: javascript reactjs react-redux

我使用ReactJSwatchify关注babelify的教程。我使用react@16版本,我知道React.createReactClass已被弃用。我需要临时修复以消除Uncaught ReferenceError: createReactClass is not defined错误。

我已经安装了create-react-class个包。我的package.json是:

{
  "main": "index.js",
  "scripts": {
    "build": "watchify src/app.js  -o public/bundle.js -t [ babelify --presets [ react es2015 ] ]",
    "server": "cd public; live-server --port=1234 --entry-file=index.html"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-react": "^6.24.1",
    "babelify": "^8.0.0",
    "live-server": "^1.2.0",
    "watchify": "^3.9.0"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-3": "^6.24.1",
    "create-react-class": "^15.6.2",
    "react": "16.0.0",
    "react-dom": "16.0.0",
    "react-scripts": "1.0.16"
  }
} 

我的app.js

const Sidebar =  createReactClass({
  render(){
    let props = this.props;

    return (<div className='sidebar'>
      <h2> All Decks </h2>
      <ul>
      {props.decs.map((deck, i)=> 
        <li key={i}> {deck.name}</li>
      )}
      </ul>
      { props.addingDeck && <input ref='add'/> }
    </div>);
  }
});
ReactDOM.render(<App>
  <Sidebar decks={[ { name: 'Deck 1' }]} addingDeck={false} />
</App>, document.getElementById('root'));

如何让createReactClass工作? 谢谢

编辑:我无法导入使用&#34; var createReactClass = require('create-react-class');&#34; 。可能是文件不是jsx文件。

1 个答案:

答案 0 :(得分:1)

对于有状态组件,您希望从React扩展Component类...

import React, {Component} from 'react'
class Sidebar extends Component {
  render(){
    let props = this.props;

    return (<div className='sidebar'>
      <h2> All Decks </h2>
      <ul>
      {props.decs.map((deck, i)=> 
        <li key={i}> {deck.name}</li>
      )}
      </ul>
      { props.addingDeck && <input ref='add'/> }
    </div>);
  }
});
ReactDOM.render(<App>
  <Sidebar decks={[ { name: 'Deck 1' }]} addingDeck={false} />
</App>, document.getElementById('root'));