SlideShare a Scribd company logo
1 of 19
Download to read offline
DIVE INTO STYLED COMPONENTS
What are styled components?
What do they solve?
Styles encapsulation
Mapping between js and css
Dynamic styling
Where can we use them
React
vue.js
react native
traditional dom
Example usage
import styled from 'styled-components';
const HeaderText = styled.h1`
font-size: ${ props => props.large ? 32 : 24 }px;
color: wheat;
text-transform: uppercase;
`;
export default () => (
<HeaderText large>Looks Cool</HeaderText>
);
API overview
import styled, { keyframes, injectGlobal, css } from 'styled-c
const Div1 = styled('div')``;
const Div2 = styled.div``;
const Div3 = Div3.extend``;
const Link = Div3.withComponent('a');
const Img = styled.img.attrs({ alt: 'custom iamge' })``;
injectGlobal`
body {
background-color: blue;
}
`;
const fadeIn = css`
100% CSS SUPPORT
AMPERSAND USAGE
Ampersands (&) get replaced by our generated, unique
classname for that styled component, making it easy
to have complex logic.
styled.a`
font-size: 20px;
&:hover {
color: red;
}
`;
WHAT IS THAT WEIRD `` THING???
"TAGGED TEMPLATE LITERAL"
console.log(`This is fine.`); // => "This is fine."
console.log`This is better.`; // => ["This is better."]
const status = "wizardry";
console.log`This is ${status}.` // => ["This is", "."], "wiz
How syled-components use Tagged Template Literals
const properties = {
status: "AMAZING"
};
function transformStrings(strings, ...args) {
return strings.map((string, i) => {
const correspondingFunction = args[i];
if (correspondingFunction && typeof correspondingFunction
return string + correspondingFunction(properties);
} else {
return string;
}
})
}
How it fits in React ecosystem
1. 1. In "componentWillMount", it checks if component
will change dynamically based on props
2. 2. In "componentWillReceiveProps", it recalculates
styles, and re-inject them to head (if condition 1. is
true)
3. 2. In "componentWillUnmount", it removes any
listeners, to avoid memory leaks
PROBLEMS AND
CAVEATS
INTERPOLATION
Code can be unreadable when not managed correctly,
due to a lot of interpolated strings
DEBUGGING IN DEVTOOLS
When using a lot of 'div' elements, class names don't
hint, what we are looking at, because they are random
strings
PERFORMANCE
Misunderstanding of how this library works, can result
in performance problems or weird bugs, like creating
new class an styles for component on each re-render
Using transitions with dynamic props isn’t a good idea,
since it always needs to recalculate and paint injected
styles on each transition trigger.
TIGHT COUPLING WITH REACT (OR
ALTERNATIVES)
It would be hard to migrate from currently used
framework or reuse your component in another
project.
LINTING
It works, but it's buggy, which makes it somewhat
useless
THANK YOU!
THAT'S ALL FOR TODAY

More Related Content

What's hot

What's hot (20)

ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
React js
React jsReact js
React js
 
State management in react applications (Statecharts)
State management in react applications (Statecharts)State management in react applications (Statecharts)
State management in react applications (Statecharts)
 
Introduction to ReactJS
Introduction to ReactJSIntroduction to ReactJS
Introduction to ReactJS
 
React Context API
React Context APIReact Context API
React Context API
 
Angular
AngularAngular
Angular
 
React Native
React NativeReact Native
React Native
 
React js
React jsReact js
React js
 
Why Vue.js?
Why Vue.js?Why Vue.js?
Why Vue.js?
 
React Hooks
React HooksReact Hooks
React Hooks
 
React native
React nativeReact native
React native
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to React
 
Reactjs
ReactjsReactjs
Reactjs
 
AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Angular data binding
Angular data binding Angular data binding
Angular data binding
 
ReactJS Tutorial For Beginners | ReactJS Redux Training For Beginners | React...
ReactJS Tutorial For Beginners | ReactJS Redux Training For Beginners | React...ReactJS Tutorial For Beginners | ReactJS Redux Training For Beginners | React...
ReactJS Tutorial For Beginners | ReactJS Redux Training For Beginners | React...
 
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
 
Jetpack compose
Jetpack composeJetpack compose
Jetpack compose
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & Answers
 

Similar to Styled components presentation

react-slides.pdf gives information about react library
react-slides.pdf gives information about react libraryreact-slides.pdf gives information about react library
react-slides.pdf gives information about react library
janet736113
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
CodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilderCodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilder
Andres Almiray
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Asp.net MVC - Course 2
Asp.net MVC - Course 2Asp.net MVC - Course 2
Asp.net MVC - Course 2
erdemergin
 

Similar to Styled components presentation (20)

react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
 
react-slides.pdf
react-slides.pdfreact-slides.pdf
react-slides.pdf
 
react-slides.pdf gives information about react library
react-slides.pdf gives information about react libraryreact-slides.pdf gives information about react library
react-slides.pdf gives information about react library
 
Software System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptxSoftware System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptx
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
iOS best practices
iOS best practicesiOS best practices
iOS best practices
 
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web TechnologyInternet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-9) [React.js] | NIC/NIELIT Web Technology
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web components
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
Angular 2 for Java Developers
Angular 2 for Java DevelopersAngular 2 for Java Developers
Angular 2 for Java Developers
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
CodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilderCodeMash - Building Rich Apps with Groovy SwingBuilder
CodeMash - Building Rich Apps with Groovy SwingBuilder
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Asp.net MVC - Course 2
Asp.net MVC - Course 2Asp.net MVC - Course 2
Asp.net MVC - Course 2
 
React hooks
React hooksReact hooks
React hooks
 
Design Patterns in Cocoa Touch
Design Patterns in Cocoa TouchDesign Patterns in Cocoa Touch
Design Patterns in Cocoa Touch
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Styled components presentation

  • 1.
  • 2. DIVE INTO STYLED COMPONENTS
  • 3. What are styled components?
  • 4. What do they solve? Styles encapsulation Mapping between js and css Dynamic styling
  • 5. Where can we use them React vue.js react native traditional dom
  • 6. Example usage import styled from 'styled-components'; const HeaderText = styled.h1` font-size: ${ props => props.large ? 32 : 24 }px; color: wheat; text-transform: uppercase; `; export default () => ( <HeaderText large>Looks Cool</HeaderText> );
  • 7. API overview import styled, { keyframes, injectGlobal, css } from 'styled-c const Div1 = styled('div')``; const Div2 = styled.div``; const Div3 = Div3.extend``; const Link = Div3.withComponent('a'); const Img = styled.img.attrs({ alt: 'custom iamge' })``; injectGlobal` body { background-color: blue; } `; const fadeIn = css`
  • 9. AMPERSAND USAGE Ampersands (&) get replaced by our generated, unique classname for that styled component, making it easy to have complex logic. styled.a` font-size: 20px; &:hover { color: red; } `;
  • 10. WHAT IS THAT WEIRD `` THING??? "TAGGED TEMPLATE LITERAL" console.log(`This is fine.`); // => "This is fine." console.log`This is better.`; // => ["This is better."] const status = "wizardry"; console.log`This is ${status}.` // => ["This is", "."], "wiz
  • 11. How syled-components use Tagged Template Literals const properties = { status: "AMAZING" }; function transformStrings(strings, ...args) { return strings.map((string, i) => { const correspondingFunction = args[i]; if (correspondingFunction && typeof correspondingFunction return string + correspondingFunction(properties); } else { return string; } }) }
  • 12. How it fits in React ecosystem 1. 1. In "componentWillMount", it checks if component will change dynamically based on props 2. 2. In "componentWillReceiveProps", it recalculates styles, and re-inject them to head (if condition 1. is true) 3. 2. In "componentWillUnmount", it removes any listeners, to avoid memory leaks
  • 14. INTERPOLATION Code can be unreadable when not managed correctly, due to a lot of interpolated strings
  • 15. DEBUGGING IN DEVTOOLS When using a lot of 'div' elements, class names don't hint, what we are looking at, because they are random strings
  • 16. PERFORMANCE Misunderstanding of how this library works, can result in performance problems or weird bugs, like creating new class an styles for component on each re-render Using transitions with dynamic props isn’t a good idea, since it always needs to recalculate and paint injected styles on each transition trigger.
  • 17. TIGHT COUPLING WITH REACT (OR ALTERNATIVES) It would be hard to migrate from currently used framework or reuse your component in another project.
  • 18. LINTING It works, but it's buggy, which makes it somewhat useless