Master React With Design Patterns
Master React With Design Patterns
React With
Design Patterns
@dinukanilupul Next
React design patterns help developers solve common problems when building
components by simplifying state management, logic, and element composition.
Common patterns like custom hooks, higher-order components (HOCs), and
prop-based rendering improve code consistency, reusability, and scalability.
Using these patterns also makes the code easier to read, maintain, and
collaborate on.
@dinukanilupul Next
HOC Pattern
HOC (Higher Order Component) is a composition pattern for reusing any logic
between components. It takes a component and returns a new component with
upgraded features.
@dinukanilupul Next
Explanation
In this example, “withLogging” is the HOC that logs a message each time the
component renders. And “MyComponent” is the component wrapped by the
HOC to include the logging behavior. Basically, now “MyComponent” has added
new functionality to log message.
@dinukanilupul Next
Don’t Use When,
@dinukanilupul Next
Compound Components Pattern
Compound Component Pattern allows a main component to contain multiple
child components. You can choose which child components to use and control
their order. This gives you flexibility and control over the component's structure
and behavior.
@dinukanilupul Next
Dropdown is the main component, and Dropdown.Toggle,
Dropdown.Menu, and Dropdown.Item are its child components. In the App
component, you control the menu’s visibility with isOpen and handle item
clicks. This gives you flexible control over the dropdown’s structure and
behavior.
@dinukanilupul Next
When to use this pattern?
When you need a component with flexible child elements that users
can control and arrange.
When multiple child components share behavior or state.
When creating reusable, customizable UI elements like tabs,
dropdowns, or forms.
@dinukanilupul Next
Custom Hook Pattern
Hooks encapsulate reusable logic into functions. In React, hooks are JavaScript
functions, with many built-in options available. You can also create custom hooks
to share logic between components.
@dinukanilupul Next
When to use this pattern?
Use custom hooks when you need to share logic between multiple
components.
To make your code more cleaner by seperating logics from UI
Manage complex stateful logic outside components for better
readabiity
Encapsulate side effects, like data fetching or subscriptions, into a
custom hook for reuse.
@dinukanilupul Next
Extensible Styles Pattern
This pattern will enable you to stylish your components in more flexible way.
Here we use dynamic CSS properties, instead directly code styles in the
components. So styles can be extended and modified later we want.
@dinukanilupul Next
Explanation
Button component has some base styles, like padding and background color,
but you can customize them by passing className or style props. It merges the
base styles with any new styles you provide. This lets you easily tweak the
button’s appearance without changing the component itself.
@dinukanilupul Next
Don’t Use When,
@dinukanilupul Next
Props Getters Pattern
Props Getters Pattern lets a component provide a function to get the props
needed for an element. It manages some logic while allowing the user to
customize or extend the props. This makes the component flexible and reusable.
@dinukanilupul Next
When to use this pattern?
@dinukanilupul Next
Render Props Pattern
Render Props Pattern lets a component use a function (passed as a prop) to
decide what to render. This allows you to share logic between components
without duplicating code. The component doesn't hardcode the output but
delegates it to the passed function.
@dinukanilupul Next
Explanation
In this example, DataFetcher is the component with a render prop. It passes the
data to the function provided by the App component, which controls how that
data is displayed (in this case, inside an <h1> tag).
@dinukanilupul Next
State Initializer Pattern
State Initializer Pattern in React is when you initialize a component's state using
props or an external function during the initial render. This allows more flexible
state setup, especially if the state depends on props or requires complex logic for
initialization.
@dinukanilupul Next
When to use this pattern?
@dinukanilupul Next
Control Props Pattern
Control Props Pattern in React is when a component's state is controlled by its
parent through props. The parent component passes down values and callbacks
to control the behavior of the child component, giving the parent full control
over the child's state and actions.
@dinukanilupul Next
Explanation
Toggle component receives isOn and onToggle as control props from the
parent. The parent (App) manages the state and passes it down, allowing full
control over when the toggle is ON or OFF. This keeps the logic in the parent,
while Toggle just displays and triggers the change.
@dinukanilupul Next
State Reducer Pattern
State Reducer Pattern in React is when a component’s state and its update
logic are separated, often using a reducer function. This allows us for more
control over how the state is managed and makes the component more flexible
by providing external control over state updates.
@dinukanilupul Next
When to use this pattern?
When you need to allow external control over how state updates
occur.
Ideal for managing components with complex state transitions.
@dinukanilupul Next
Follow For More !
Learn Together, Grow Together
Dinuka Nilupul
@dinukanilupul
@dinukanilupul