Components, Composition, and Reusability
Just one
huge component
Just one
huge component
Too many responsibilities
Just one
huge component
Too many responsibilities
Too many props
Just one
huge component
Too many responsibilities
Too many props
Difficult to reuse
Just one
huge component
Too many responsibilities
Too many props
Difficult to reuse
Complex code, hard to understand
Many small
components
Too many responsibilities
Too many props
Difficult to reuse
Complex code, hard to understand
Many small
components
Too many responsibilities
Too many props
Difficult to reuse
Complex code, hard to understand
100 mini components
Confusing codebase
Many small
components
Too many responsibilities
Too many props
Difficult to reuse
Complex code, hard to understand
100 mini components
Confusing codebase
Too abstracted
Many small
components
Too many responsibilities
Too many props
Difficult to reuse
Complex code, hard to understand
100 mini components
Confusing codebase
Too abstracted
Right balance!
Logical Separation
Reusable
Low complexity
Start with a big component
Start with a big component
then split it into smaller components
Start with a big component
then split it into smaller components
Start with a big component
then split it into smaller components
unless the whole chunk is reused - and you are sure - you don't need to focus on reusability
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
Is the component doing TOO MANY things?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
Is the component doing TOO MANY things?
or have TOO MANY PROPS?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
Is the component doing TOO MANY things?
or have TOO MANY PROPS?
TOO MANY states?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
Is the component doing TOO MANY things?
or have TOO MANY PROPS?
TOO MANY states?
is the code TOO COMPLEX / CONFUSING?
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
Is the component doing TOO MANY things?
or have TOO MANY PROPS?
TOO MANY states?
is the code TOO COMPLEX / CONFUSING?
time to make a new component!
1. Logical separation of
content / layout
2. Reusability
3. Responsibilities
and complexity
4. Personal
coding style
Does the component contain pieces of content or layout that don't belong together?
time to make new component!
Is it possible to reuse part of the component?
Do you want or need to reuse it?
time to make a new component!
Is the component doing TOO MANY things?
or have TOO MANY PROPS?
TOO MANY states?
is the code TOO COMPLEX / CONFUSING?
time to make a new component!
Do you prefer smaller function / components?
function makeCoffee() {}
function makeCoffee(beans) {
const stampedBeans = stamp(beans)
}
function makeCoffee(beans) {
const stampedBeans = stamp(beans)
const extractedCoffee = extract(stampedBeans)
}
function makeCoffee(beans) {
const stampedBeans = stamp(beans)
const extractedCoffee = extract(stampedBeans)
return extractedCoffee
}
function makeCoffee(beans) {
...
}
const COFFEE_BEAN_GRAMS = 60
const coffee = makeCoffee(COFFEE_BEAN_GRAMS)
function PropertyCard(props) {
return <div> ... </div>
}
function PropertyCard(props) {
return <div> ... </div>
}
function App() {
return <div className="grid grid-cols-3 gap-4">
<PropertyCard ... />
<PropertyCard ... />
<PropertyCard ... />
</ div>
}
Guidelines to making components
Guidelines to making components
Beware that creating a new component is same as creating a new abstraction
Guidelines to making components
Beware that creating a new component is same as creating a new abstraction
Name a component according to what it does and what it displays
Guidelines to making components
Beware that creating a new component is same as creating a new abstraction
Name a component according to what it does and what it displays
components and functions name can be long!
Guidelines to making components
Beware that creating a new component is same as creating a new abstraction
Name a component according to what it does and what it displays
components and functions name can be long!
Co-locate related components inside the same file
Guidelines to making components
Beware that creating a new component is same as creating a new abstraction
Name a component according to what it does and what it displays
components and functions name can be long!
Co-locate related components inside the same file
It's okay for components to have many different sizes
Categories of Components
Categories of Components
Categories of Components
Stateless /
Presentational
Components
Categories of Components
Stateless /
Presentational
Components
Stateful
Components
Categories of Components
Stateless /
Presentational
Components
Stateful
Components
Structural
Components
Categories of Components
Stateless /
Presentational
Components
Stateful
Components
Structural
Components
NO STATES
Receive props and simply present data
and contents
Small and reusable
Categories of Components
Stateless /
Presentational
Components
Stateful
Components
Structural
Components
NO STATES
Receive props and simply present data
and contents
Small and reusable
HAVE STATES
Can still be reusable
Categories of Components
Stateless /
Presentational
Components
Stateful
Components
Structural
Components
NO STATES
Receive props and simply present data
and contents
Small and reusable
HAVE STATES
Can still be reusable
Pages / layouts / screens of the app
Result of composition
can be huge and non-reusable
Component composition
Component composition
USING A COMPONENT
COMPONENT COMPOSITION
Component composition
USING A COMPONENT
COMPONENT COMPOSITION
<Success /> is inside Modal
Component composition
USING A COMPONENT
COMPONENT COMPOSITION
<Success /> is inside Modal
cannot reuse
Component composition
USING A COMPONENT
COMPONENT COMPOSITION
<Success /> is inside Modal
cannot reuse
Component composition
USING A COMPONENT
COMPONENT COMPOSITION
<Success /> is inside Modal
cannot reuse
Passing component as a prop
Passing component as a prop
Passing component as a prop
Passing component as a prop
Passing component as a prop
Passing component as a prop
Passing component as a prop