3

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?

2
  • Where are you defining classNames ? Commented Jul 5, 2018 at 18:10
  • @Derek I don't know. As I said this code is not mine. Just copied it. Probably it is not defined. I am reading e-book and downloaded all their source code. This is only one js code which I found on this chapter.
    – kust kust
    Commented Jul 5, 2018 at 18:13

4 Answers 4

8

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.

7
  • do you know difference between npm i classnames --save and npm i -S classnames? I tried both and I don't see any difference
    – kust kust
    Commented Jul 5, 2018 at 18:26
  • They're the exact same statement! -S is just shorthand for --save, the same way npm i is just shorthand for npm install Commented Jul 5, 2018 at 18:29
  • Thanks. I tried the console command and added import on the top of my app.js file. but my chrome 67 browser shows Uncaught Syntaxerror..
    – kust kust
    Commented Jul 5, 2018 at 18:36
  • Should I do npm install before npm install -S classnames?
    – kust kust
    Commented Jul 5, 2018 at 18:38
  • 1
    added type=text/babel to my script. Thanks. Error is disappeared!
    – kust kust
    Commented Jul 5, 2018 at 18:54
5

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
  • In my react experiences - react/gatsby for react/react native - i've never had to import classNames... Commented Jul 5, 2018 at 18:14
  • 1
    @StephenTetreault well its not native to react so I'm not sure what you're using that bundles it with it Commented Jul 5, 2018 at 18:14
  • I did npm i -S classnames. The name of the js file is app.js. I added import on the top of the app.js and my browser shows unexpected identifier.
    – kust kust
    Commented Jul 5, 2018 at 18:24
1

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

1
0
  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 ./

Not the answer you're looking for? Browse other questions tagged or ask your own question.