我使用ReactJS
和watchify
关注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文件。
答案 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'));