Middle+
What is a HOC (Higher Order Component)? What programming patterns are used in React?
sobes.tech AI
Answer from AI
HOC (Higher Order Component) in React is a function that takes a component and returns a new component with extended or modified behavior. It is a way to reuse logic between components.
Example of HOC:
function withLogger(WrappedComponent) {
return function(props) {
console.log('Rendering component', WrappedComponent.name);
return <WrappedComponent {...props} />;
};
}
Main programming patterns in React:
- Higher-Order Components (HOC) — for reusing logic.
- Render Props — passing a function to control what is rendered.
- Containers and Presentational Components — separating logic and display.
- Hooks — using functions to manage state and side effects.
- Component Composition — building complex interfaces from simple ones.
These patterns help create flexible, scalable, and maintainable applications.