Declarative UI Mapping in TypeScript and JavaScript

Do you do this in your UI components or any use case? 👀 Using if / else inside a function to decide what to render or what to return isn’t wrong, especially for simple or one-off logic. But when the output is predictable (status codes, enums, types, variants, modes, roles, etc.), a data-driven mapping often scales better. By moving UI decisions into an object map: • the intent is clearer • the component stays cleaner • adding new cases doesn’t mean adding more branches This is just a basic example (status), the same pattern applies to any predictable case like role, type, variant, mode, step, state, and more. Conditionals are fine but when your UI grows, declarative mappings keep things easier to read and maintain. This pattern isn’t limited to frontend UI. The same idea applies in full-stack or backend code. #TypeScript #JavaScript #React #Frontend #CleanCode #UIEngineering #WebDevelopment #ProgrammingTips #ReactJS #NextJS #FullStack #Backend

  • Side-by-side comparison infographic showing two approaches to UI rendering in React with TypeScript. The left side, labeled “Do you do this?”, displays a function using if-else conditions to return different badge props based on a numeric status, marked with a red X. Below it are bullet points describing it as imperative, logic-heavy, growing vertically with multiple else-if statements, harder to scan, and easy to forget a case. The right side, labeled “Do this instead.”, shows an object map that maps status values to badge props, marked with a green check. Below it are bullet points describing it as declarative, data-driven, easy to extend, easy to read, and providing safer defaults. Both sides include sample React component usage demonstrating the difference.

To view or add a comment, sign in

Explore content categories