- Higher Order Components
- Accordion (Bootstrap)
- Controlled Components
- New Docs: Controlled & Uncontrolled Components
- Lifting the State Up
- Props Drilling Problem
- Ways to Avoid Props Drilling
A Higher-Order Component (HOC) in React is a function that takes a component as an argument and returns a new component that enhances the original component.
- HOCs enhance a component without modifying its original code.
- They wrap the original component and add extra functionality.
- They are pure functions, meaning they don’t alter the input component but return a new enhanced one.
- Reusability → Avoids redundant code across components.
- Flexibility → Can accept additional arguments for customization.
- Useful for authentication, error handling, logging, and performance tracking.
function withRouter(ExistingComponent) {
return function (props) {
const location = useRouter();
return <ExistingComponent {...props} location={location} />;
};
}The Data Layer in a React app consists of:
- State
- Props
- Local Variables
- Any data the app interacts with
It serves as the power source for the UI layer.
The UI Layer consists of JSX that gets rendered in the browser DOM.
- It is static and depends on the Data Layer for dynamic updates.
Lifting the state up means moving the state from child components to a common parent component to maintain synchronization.
- Ensures consistent state updates across multiple components.
- Useful when multiple components need to share a single source of truth.
- Instead of each Accordion item having its own state, we move the state to the parent and pass it down via props.
- State is controlled by the parent component.
- Uses props to get and set values.
- Example: A menu category component controlled by its parent container.
- Manages its own state internally.
- Not dependent on external control.
- Example: A form input where React doesn’t track the state.
Props Drilling occurs when data is passed down multiple levels of components unnecessarily, even to components that don’t need it.
- Makes components tightly coupled.
- Unnecessary prop forwarding increases complexity.
- Used to create a central/global place to store values.
- Wrapped around parts of the app to provide context data.
- Uses
<SomeContext.Provider value={data}>.
- Used to access values from the Context Provider.
- Uses
<SomeContext.Consumer>oruseContext(SomeContext).
No, it doesn’t take the default value. Instead:
- The value becomes undefined, causing errors.
- If the provider is completely omitted, components will use the default context value.
const ThemeContext = createContext("Default");const User = () => {
const context = useContext(ThemeContext);
return <h2>User: {context}</h2>;
};const App = () => {
return (
<div>
{/* Context Provider with a value */}
<ThemeContext.Provider value={{ context: "User1" }}>
<User /> {/* Output: User1 */}
</ThemeContext.Provider>
{/* Without Context Provider, fallback to default */}
<User /> {/* Output: Default */}
</div>
);
};