我是 React 开发的新手,并试图了解 classNames 如何在 React 中工作。
这是我书中的react代码。我刚刚复制了它。
const MOUNT = document.getElementById('root');
class App extends React.Component {
render() {
const klasses = classNames({
box: true, // always apply the box class
alert: this.props.isAlert, // if the prop is set
severity: this.state.onHighAlert, // with state
timed: false // never apply this class
});
return React.createElement(
'div',
{className: klasses},
React.createElement('h1', {}, 'Hello world')
);
}
}
ReactDOM.render(React.createElement(App, {}), MOUNT);
我将此代码的脚本文件包含到 html 中,浏览器控制台显示此类错误。
app.js:4 Uncaught ReferenceError: classNames is not defined
at App.render (app.js:4)
at finishClassComponent (react-dom.js:11320)
at updateClassComponent (react-dom.js:11297)
at beginWork (react-dom.js:11676)
at performUnitOfWork (react-dom.js:13644)
at workLoop (react-dom.js:13753)
at HTMLUnknownElement.callCallback (react-dom.js:1527)
at Object.invokeGuardedCallbackDev (react-dom.js:1566)
at invokeGuardedCallback (react-dom.js:1423)
at performWork (react-dom.js:13871)
问题是什么?