简体   繁体   中英

How to add more than one class on React JSX?

It works with one class only, but it won't compile with 2 or more classes declared.

function Logo() {
    return (
        <div className={logo}>
            <img src={logo} id={logo} className={logo second} alt={"..."}/>
        </div>
    );
}

Error:

  Line 21:55:  Parsing error: Unexpected token, expected "}"

  19 |     return (
  20 |         <div className={logo}>
> 21 |             <img src={logo} id={logo} className={logo second} alt={"..."}/>
     |                                                       ^
  22 |         </div>
  23 |     );
  24 | }

The className prop takes a single string, in the same way that the class attribute would in HTML.

It looks like both of your classes are stored in variables, so you can put them together like so (2 methods):

Template Strings

<img src={logo} id={logo} className={`${logo} ${second}`} alt={"..."}/>

String Concatenation

<img src={logo} id={logo} className={logo + " " + second} alt={"..."}/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM