How to use styled-component
#HelloFrontend #HelloErmine #HelloWorld2021
พื้นฐานการใช้งาน styled-components
การสร้างตัวเเปร styled-components
import styled-components
สร้าง component ขึ้นมา
กำหนด property ไว้ภายในเครื่องหมาย ` ` (grave accent) และปิดท้ายแต่ละ property ด้วยเครื่องหมาย ; (semicolon)
เมื่อสร้าง styled-components เสร็จแล้ว จะเหมือนเราได้ tag ใหม่ที่มีชื่อตามที่เรากำหนดให้ const
เรียกใช้ styled-components ที่ต้องการ โดยใช้ tag ที่มีชื่อเดียวกับ const

การใช้งาน styled-components ในรูปเเบบต่าง ๆ
Inheritance
Inheritance หรือ การสืบทอด คือ การสืบทอดลักษณะจาก component พ่อ (Parent) ไปยัง component ลูก (Child)

CSS selector
มีหลักการเหมือนการใช้ CSS selector กับ HTML เลย เช่น การใช้ CSS selector ชนิดที่เป็น class selector จะมีการใช้เครื่องหมาย . นำหน้าส่วนที่ต้องการระบุว่าเป็น class ที่ภายในมีชุดคำสั่ง CSS อยู่ และในการเรียกใช้ชุดคำสั่งนั้น ใน 1 tag จะเรียกใช้กี่ชุดคำสั่งก็ได้
ในการใช้ CSS Selector กับ styled-components จะต้องเขียน CSS Selector อยู่ใน styled-components และมักจะใช้เครื่องหมาย & นำหน้าตัวที่ใช้บ่งบอกชนิดของ CSS selector อยู่ด้วย แต่จะไม่มีก็ได้
ใช้ . นำหน้าเพื่อบอกว่าเป็นส่วนของ class และใช้ props ที่มีชื่อว่า className ในการเรียกใช้

ใช้ element name หรือ ชื่อ tag ในการเรียกใช้ได้เลย

ใช้ :hover เพื่อบอกว่าเป็นส่วนของ hover


Last updated
Was this helpful?