0

I have some HTML that needs custom attributes with no value dependent on the situation.. for example these are all valid:

<div className="playlist-row" selected>
<div className="playlist-row" active>   
<div className="playlist-row">

i tried doing something like

let customAttr = {'active'};  // incorrect syntax
let customAttr = {'active': true};  //doesn't work
let customAttr = {'active': ''};  //doesn't work

<div className="playlist-row" {...customAttr}>

Any ideas of how I can get this to work?

1 Answer 1

1

I believe this should do it - in your render() function:

render() {
    let customAttr = "active"

    return (
        <div className={"playlist-row " + customAttr}>
    )
}

or alternately, you could avoid string concatenation...

<div className={`playlist-row ${customAttr}`}>
Sign up to request clarification or add additional context in comments.

4 Comments

unfortunately didn't work, ty for trying though. i think because active isn't part of the class name? im just guessing..
@user1189352 Hmm... I've edited my response to make it a more fleshed-out render function - does yours basically look like this? Defining customAttr, then returning the element?
oh snap it worked. i just had a typo on my end. thank you!
@user1189352 Great! Glad that helped. Don't forget to accept the answer - I think this is a common problem, will be helpful to the community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.