Tagged Template Literal
sytled-componentsλ₯Ό μ¬μ©ν΄λ΄€λ€λ©΄ λ€μκ³Ό κ°μ μ½λλ₯Ό λ³Έμ μ΄ μμ κ²μ΄λ€.
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: #bf4f74;
`;
λ€μκ³Ό κ°μ μ½λλ₯Ό Tagged Template Function , tag function
μ΄λΌκ³ λΆλ₯Έλ€.
Template literals (Template strings)
κΈ°λ³Έ ν¬νλ¦Ώ 리ν°λ΄λ‘ μμ±νλ λ¬Έμμ΄μ λ€μκ³Ό κ°λ€.
const a = 5;
const b = 10;
console.log(`Fifteen is ${a + b} and
not ${2 * a + b}.`);
// "Fifteen is 15 and
// not 20."
Tagged templates
λ³΄λ€ κ³ κΈ ννμ ν νλ¦Ώ 리ν°λ΄μ νκ·Έκ° μ§μ λ ν νλ¦Ώμ λλ€.
νκ·Έλ₯Ό μ¬μ©νλ©΄ ν¨μλ₯Ό μ¬μ©νμ¬ ν νλ¦Ώ 리ν°λ΄μ ꡬ문 λΆμν μ μμ΅λλ€. νκ·Έ ν¨μμ 첫 λ²μ§Έ μΈμλ λ¬Έμμ΄ κ°μ λ°°μ΄μ ν¬ν¨ν©λλ€. λλ¨Έμ§ μΈμλ ννμκ³Ό κ΄λ ¨μ΄ μμ΅λλ€.
κ·Έλ¬λ©΄ νκ·Έ ν¨μλ μ΄λ¬ν μΈμμ λν΄ μνλ μ°μ°μ μννκ³ μ‘°μλ λ¬Έμμ΄μ λ°νν μ μμ΅λλ€. (λλ λ€μ μμ μ€ νλμ μ€λͺ λ κ²μ²λΌ μμ ν λ€λ₯Έ κ²μ λ°νν μλ μμ΅λλ€.)
νκ·Έμ μ¬μ©λλ ν¨μμ μ΄λ¦μ μνλ λλ‘ μ§μ ν μ μμ΅λλ€.
const person = 'Mike';
const age = 28;
function myTag(strings, personExp, ageExp) {
const str0 = strings[0]; // "That "
const str1 = strings[1]; // " is a "
const str2 = strings[2]; // "."
const ageStr = ageExp < 100 ? 'youngster' : 'centenarian';
// We can even return a string built using a template literal
return `${str0}${personExp}${str1}${ageStr}${str2}`;
}
const output = myTag`That ${person} is a ${age}.`;
console.log(output);
// That Mike is a youngster.