Style Props Merging
ReactJS
Medium
6 views
Problem Description
Create a Box component that merges default styles with a style prop.
Output Format
Render a React component.
Constraints
Use object spread for style merging.
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!
No comments yet. Start the discussion!