我遵循有关使用ReactJSwatchifybabelify的教程。我使用的是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工作?
谢谢

编辑:我不能使用“ var createReactClass = require('create-react-class');”导入。可能是file不是jsx文件。

最佳答案

对于有状态组件,您想从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'));

关于javascript - 未捕获的ReferenceError:未定义createReactClass,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47061672/

10-12 18:47