classNames is not defined

China☆狼群 提交于 2021-02-19 06:50:14

问题


I am newbie in react development and trying to understand how classNames work in react.

This is the react code from my book. I just copied it.

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);

I included script file with this code to html and browser console shows such error.

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)

what is the problem?


回答1:


So, I think you'll want to install the classnames npm module to bind conditional classes defined in an object.

https://www.npmjs.com/package/classnames

npm i classnames --save

Next you'll want to require that in your .js / .jsx file before use.

import classNames from 'classnames';

Then use the module to bind your classes to the React Element.

import classNames from 'classnames';
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);

Hope this helps, looks like Tholle got there first.




回答2:


classnames is a library that you need to install and import into your module. Try npm i -S classnames in your shell, and then import classNames from 'classnames'; at the top of your JavaScript file.




回答3:


The answers above work but you need change the line that imports app.js in your index.html file:

<script type="module" "src="app.js"></script>

The suggestions above worked for me (I am reading the same React book as your).




回答4:


  1. Per the comment from @kust kust you need to add type="text/babel" to the <script> tag for app.js.
  2. You need to update all paths to be relative ... from / to ./


来源:https://stackoverflow.com/questions/51197486/classnames-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!