ReactJS Program to Reusable Button Variants with Explanation
ReactJS
Easy
Component Design
26 views
1 min read
85 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around variant, component, button. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create a Button component with variants: primary and ghost.
Input Format
No input.
Output Format
Render a React component.
Constraints
Use a single component with a variant prop.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React from 'react';
export function Button({ variant = 'primary', disabled = false, children, onClick }) {
const base = {
padding: '10px 14px',
borderRadius: 12,
border: '1px solid transparent',
fontWeight: 600,
cursor: disabled ? 'not-allowed' : 'pointer'
};
const stylesByVariant = {
primary: { background: '#0b5', color: '#fff' },
ghost: { background: 'transparent', color: '#124', border: '1px solid #ddd' }
};
const style = { ...base, ...stylesByVariant[variant], opacity: disabled ? 0.6 : 1 };
return (
<button type='button' style={style} disabled={disabled} onClick={onClick}>
{children}
</button>
);
}
export default function App() {
return (
<div style={{ display: 'flex', gap: 12, padding: 16 }}>
<Button onClick={() => {}}>Start on meetcode</Button>
<Button variant='ghost' onClick={() => {}}>View topics</Button>
</div>
);
}
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Create a Button component with variants: primary and ghost.
Input / Output
Output
Render a React component.
Constraints
Use a single component with a variant prop.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
import React from 'react';
export function Button({ variant = 'primary', disabled = false, children, onClick }) {
const base = {
padding: '10px 14px',
borderRadius: 12,
border: '1px solid transparent',
fontWeight: 600,
cursor: disabled ? 'not-allowed' : 'pointer'
};
const stylesByVariant = {
primary: { background: '#0b5', color: '#fff' },
ghost: { background: 'transparent', color: '#124', border: '1px solid #ddd' }
};
const style = { ...base, ...stylesByVariant[variant], opacity: disabled ? 0.6 : 1 };
return (
<button type='button' style={style} disabled={disabled} onClick={onClick}>
{children}
</button>
);
}
export default function App() {
return (
<div style={{ display: 'flex', gap: 12, padding: 16 }}>
<Button onClick={() => {}}>Start on meetcode</Button>
<Button variant='ghost' onClick={() => {}}>View topics</Button>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!