Skip to content

QuizNot attempted

Open/Closed in React

How do you add a new button style without editing the Button component's internals every time?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Accept a variant from a config map, or composition (icon, children, className) so new behaviour plugs in from outside
  • Open/Closed means open for extension, closed for modification: a variant config map or composition (icon, children, className) lets new styles plug in without touching tested internals.
  • Not another if branch: that is modification every time.
  • Not copy-paste PurpleButton: duplicates logic that will drift.
  • Not global !important: bypasses the component and breaks the cascade for everyone.