Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow template literals directly in JSX as attributes. #15442

Closed
Y-Less opened this issue Apr 18, 2019 · 7 comments
Closed

Allow template literals directly in JSX as attributes. #15442

Y-Less opened this issue Apr 18, 2019 · 7 comments

Comments

@Y-Less
Copy link

Y-Less commented Apr 18, 2019

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

Template literals are now the recommended way of formatting anything as a string, and the same applies to JSX:

return (
	<MyComponent className={`${classA} ${classB}`} />
);

This seems a tiny bit verbose. Not massive mind, but for every one of these you always open with {` and always close with `}. I don't know if those will render correctly, it's hard to type a literal backtick in markdown, so just in case brace-backtick and backtick-brace. Backticks are not valid in HTML or JSX, so it would be a very small QOL improvement if they could be used directly to specify attributes.

What is the expected behavior?

return (
	<MyComponent className=`${classA} ${classB}` />
);

This would be quite a special case of raw string literals. You couldn't use tags, such as:

return (
	<MyComponent className=tag`${classA} ${classB}` />
);

Without going back to the normal method:

return (
	<MyComponent className={tag`${classA} ${classB}`} />
);

And backticks anywhere but immediately starting an attribute would just be interpreted the same as now - as a literal character:

return (
	<div>
		This is a backtick: `
	</div>
);

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16 at least.

@leeseean
Copy link

you are making a joke,too young too simple,stupid brain

@Y-Less
Copy link
Author

Y-Less commented Apr 19, 2019

too simple

Sorry, my mistake, I didn't realise feature requests had to be large and complex.

@Spenc84
Copy link

Spenc84 commented Apr 19, 2019

@leeseean Your comment is quite rude and doesn't add any value to this forum. Questions asked in earnest shouldn't ever be belittled.

Y-less is simply proposing a quality of life improvement. JSX already supports string literals in the form of attribute="foo" and attribute='foo'. If there is a valid reason why React's transpiler couldn't also be updated to recognize attribute=`foo` then provide that reasoning rather than a demeaning remark.

Though it may seem a simple thing, writing attribute=`foo` certainly is cleaner than attribute={`foo`}.

@schibrikov
Copy link

Totally agree.
It would make a perfect sense.

@kunukn
Copy link
Contributor

kunukn commented Apr 21, 2019

@aweary
Copy link
Contributor

aweary commented Apr 21, 2019

The JSX syntax is not part of React and is shared by many other libraries, so the specification itself would need to be changed. There's already an open issue for this on the jsx repo so feel free to add your input there!

@aweary aweary closed this as completed Apr 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants