Access className by template literal string not working
See original GitHub issueSo I am trying to access a className based on a template literal string and it’s not working.
WHAT WORKS:
<div className={classNames(classes.box, classes.XYZcontentType)} />
WHAT DOES NOT WORK:
const prop = 'XYZ';
<div className={classNames(classes.box, classes[`${prop}contentType`])} />
I also tried:
const prop = 'XYZ';
<div className={classNames( classes.box, { [`${prop}contentType`]: true } )} />
How can I get classNames to work with template strings that are generated dynamically?
Notes: I am using React.
Issue Analytics
- State:
- Created a year ago
- Comments:7
Top Results From Across the Web
change className to using template literals - Stack Overflow
Assuming that props.classes is a string, I would write your className like this: className={`${classes}.selectionList__sites`}.
Read more >Stack your classes within template literals | by Chris Nager
The purpose of this article is to share a simple technique I use that involves template literals, a useful feature of ES2015+.
Read more >Not detecting class names with template literals #6559 - GitHub
When class names are contained in template literal strings, they are not picked up by the parser unless a trailing space is supplied....
Read more >Template Literals | CSS-Tricks
The Template Literal, introduced in ES6, is a new way to create a string. With it comes new features that allow us more...
Read more >JavaScript Template Literals - W3Schools
Template literals provide an easy way to interpolate variables and expressions into strings. The method is called string interpolation. The syntax is:.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@shiraz I created a demo in CodeSandbox. See here. The code works with a dynamically generate template string.
My guess is there’s something wrong with your application code. Can you provide an example that reproduces the issue?
@shiraz this is a react-jss and CSS question, and not relevant for this issue.