template-literals

Template literals as string content in JSX

百般思念 提交于 2020-08-19 12:08:28
问题 I'm wondering whats the best practice for strings values mixed with variables inside JSX tags, I've listed the options I'm familiar with: render() { const {totalCount} = this.state; const totalCountStr = `Total count: ${totalCount}`; return ( <div> <h1>Total count: {totalCount}</h1> // 1 <h1>`Total count: ${totalCount}`</h1> // 2 <h1>{totalCountStr}</h1> // 3 </div> ); } What's the best practice or the use cases to use them differently? Thanks! 回答1: Template literals aren't supported by React

How do tagged template literals work in JavaScript?

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-16 04:37:49
问题 Consider the following snippet: let a = (x) => console.log(x); a`1234`; // prints "Array [ "1234" ]" Why does the string, following the variable holding the anonymous function, makes the function run passing itself in an Array as argument? Is there any documentation reference for this behavior? Also, why it doesn't work when using quotes or double quotes for declaring the string literal? 回答1: Tagged template literals take the form of (strings: string[], ...values: any[]): any . In your

Ignore the indentation in a template literal, with the ESLint `indent` rule

≡放荡痞女 提交于 2020-05-25 15:54:02
问题 The ESLint rule for indent allows for you to specify which nodes are ignored, when determining whether the rule should apply to that node, using the ignoredNodes option. I've got the following code that I want to ignore with this rule: const a = b ? `c${ d }` : e Specifically, the line with d and the subsequent line are reported as having two more spaces than they should. I want to ignore those lines from the rule, but I can't figure out the node that should apply. Node types are specified in

Why can't String.raw end with a backslash?

混江龙づ霸主 提交于 2020-05-15 05:25:31
问题 String.raw can be used to create a string that contains backslashes, without having to double up those backslashes. Historically, you'd need to double up backslashes when creating a string: let str = "C:\\Program Files\\7-Zip"; console.log(str); String.raw allows your code to show the path without doubled backslashes: let str = String.raw`C:\Program Files\7-Zip`; console.log(str); The above code works fine, but today I discovered that it doesn't work if the raw string ends with a backslash:

Setting HTML Button`onclick` in template literal

喜夏-厌秋 提交于 2020-04-08 13:27:22
问题 I have an html template that i'm using template literals for. The function looks like the below // postCreator.js export const blogPostMaker = ({ title, content, address, id }, deletePost) => { const innerHtml = ` <blog-post> <h1 class='title'>${title}</h1> <p class='content'>${content}</p> <p class='address'>${address}</p> <button onclick='${() => deletePost(id)}'>Delete</button> </blog-post> ` return innerHtml } //Blog.js postHelper.getSeriesOfPosts(10) .then(resp => { resp.forEach(post =>

Setting HTML Button`onclick` in template literal

耗尽温柔 提交于 2020-04-08 13:23:46
问题 I have an html template that i'm using template literals for. The function looks like the below // postCreator.js export const blogPostMaker = ({ title, content, address, id }, deletePost) => { const innerHtml = ` <blog-post> <h1 class='title'>${title}</h1> <p class='content'>${content}</p> <p class='address'>${address}</p> <button onclick='${() => deletePost(id)}'>Delete</button> </blog-post> ` return innerHtml } //Blog.js postHelper.getSeriesOfPosts(10) .then(resp => { resp.forEach(post =>

I'm trying to list all NESTED values of an array in javascript, but get only the 1st value of each in the array

拥有回忆 提交于 2020-01-24 19:03:10
问题 I am a newbe at Template Literals, mapping and javascript. I have an array and I'm using Template Literals to insert a div into an html. I'm mapping the array of 4 albums. In the array I have lists of tracks for each album. I'm trying to list one track list for each album at a time. When I run the code I only get the 1st track of each album instead of the 1st track list of the 1st album. How can I list all the values in the first nest of the array? The code is as follows: // JavaScript

Combining ES6 unicode literals with ES6 template literals [duplicate]

笑着哭i 提交于 2020-01-14 10:10:03
问题 This question already has an answer here : ES6: Bad character escape sequence creating ASCII string (1 answer) Closed 3 years ago . If I want to print a unicode Chinese character in ES6/ES2015 javascript, I can do this: console.log(`\u{4eb0}`); Likewise, if I want to interpolate a variable into a template string literal, I can do this: let x = "48b0"; console.log(`The character code is ${ x.toUpperCase() }.`); However, it seems that I can't combine the two to print a list of, for example, 40

Combining ES6 unicode literals with ES6 template literals [duplicate]

五迷三道 提交于 2020-01-14 10:09:52
问题 This question already has an answer here : ES6: Bad character escape sequence creating ASCII string (1 answer) Closed 3 years ago . If I want to print a unicode Chinese character in ES6/ES2015 javascript, I can do this: console.log(`\u{4eb0}`); Likewise, if I want to interpolate a variable into a template string literal, I can do this: let x = "48b0"; console.log(`The character code is ${ x.toUpperCase() }.`); However, it seems that I can't combine the two to print a list of, for example, 40