654

I am new to ReactJS and JSX and I am having a little problem with the code below.

I am trying to add multiple classes to the className attribute on each li:

<li key={index} className={activeClass, data.class, "main-class"}></li>

My React component is:

var AccountMainMenu = React.createClass({
  getInitialState: function() {
    return { focused: 0 };
  },

  clicked: function(index) {
    this.setState({ focused: index });
  },

  render: function() {
    var self = this;
    var accountMenuData = [
      {
        name: "My Account",
        icon: "icon-account"
      },
      {
        name: "Messages",
        icon: "icon-message"
      },
      {
        name: "Settings",
        icon: "icon-settings"
      }
    /*{
        name:"Help &amp; Support &nbsp; <span class='font-awesome icon-support'></span>(888) 664.6261",
        listClass:"no-mobile last help-support last"
      }*/
    ];

    return (
      <div className="acc-header-wrapper clearfix">
        <ul className="acc-btns-container">
          {accountMenuData.map(function(data, index) {
            var activeClass = "";

            if (self.state.focused == index) {
              activeClass = "active";
            }

            return (
              <li
                key={index}
                className={activeClass}
                onClick={self.clicked.bind(self, index)}
              >
                <a href="#" className={data.icon}>
                  {data.name}
                </a>
              </li>
            );
          })}
        </ul>
      </div>
    );
  }
});

ReactDOM.render(<AccountMainMenu />, document.getElementById("app-container"));
simhumileco
  • 31,877
  • 16
  • 137
  • 115
Hector
  • 6,625
  • 3
  • 16
  • 18
  • I found a breif answer here https://stackoverflow.com/a/36209517/4125588, just use JavaScript to join this classes, static or dynamic, with '+' operator, remember to insert ' ' before the classes except the first one, as the real class in HTML should be like 'a b c', also space between them. – GLPease Aug 08 '17 at 01:31
  • Possible duplicate of [Passing in class names to react components](https://stackoverflow.com/questions/32230635/passing-in-class-names-to-react-components) – dashdashzako Nov 15 '18 at 10:05
  • 2
    Why don't `classNames={{foo: true, bar: true, baz: false}}` and `classNames={["foo", "bar"]}` just _work_? – Peter V. Mørch Mar 02 '20 at 09:57
  • Then why are you assigning only one class name "active" to the li element? – John McGovern Jun 30 '20 at 20:34
  • You can check out https://www.npmjs.com/package/@ivanhanak_com/react-join-classnames, where basically you can use `
    `
    – Ivan Hanák Jul 07 '20 at 19:47
  • I'm sure you've found out since then,,, And I'm not sure what you really tried to do, if it was "make the
  • active on hover" or anything dynamic, the problem isn't how you build className, it's just because you modify values in the background while you should've used a "setState" or a hook that recreate the element... – Cerberus Feb 08 '23 at 09:53