ReactJS Program to Style Props Merging with Explanation
ReactJS
Medium
Component Design
26 views
1 min read
86 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around style, prop, merging. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create a Box component that merges default styles with a style prop.
Input Format
No input.
Output Format
Render a React component.
Constraints
Use object spread for style merging.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React from 'react';
function Box({ style, children }) {
const base = { border: '1px solid #eee', borderRadius: 14, padding: 14 };
return <div style={{ ...base, ...style }}>{children}</div>;
}
export default function App() {
return (
<div style={{ padding: 16, display: 'grid', gap: 12 }}>
<Box>Default box on meetcode</Box>
<Box style={{ background: '#eafaf1' }}>Green box</Box>
</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 Box component that merges default styles with a style prop.
Input / Output
Output
Render a React component.
Constraints
Use object spread for style merging.
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).
Difficulty
Medium
ReactJS
Official Solution
import React from 'react';
function Box({ style, children }) {
const base = { border: '1px solid #eee', borderRadius: 14, padding: 14 };
return <div style={{ ...base, ...style }}>{children}</div>;
}
export default function App() {
return (
<div style={{ padding: 16, display: 'grid', gap: 12 }}>
<Box>Default box on meetcode</Box>
<Box style={{ background: '#eafaf1' }}>Green box</Box>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!