ReactJS Program to Layout Component With Props with Explanation
ReactJS
Medium
Component Design
17 views
1 min read
86 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around component, layout, prop. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create a Layout component that accepts header and footer content as props.
Input Format
No input.
Output Format
Render a React component.
Constraints
Use children for main content.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React from 'react';
function Layout({ header, footer, children }) {
return (
<div style={{ fontFamily: 'system-ui, Arial, sans-serif' }}>
<div>{header}</div>
<main style={{ padding: 16 }}>{children}</main>
<div>{footer}</div>
</div>
);
}
export default function App() {
return (
<Layout
header={<header style={{ padding: 16, borderBottom: '1px solid #eee' }}><strong>meetcode</strong></header>}
footer={<footer style={{ padding: 16, borderTop: '1px solid #eee', color: '#555' }}>Keep practicing</footer>}
>
<h1 style={{ marginTop: 0 }}>Topics</h1>
<p>Pick one and start.</p>
</Layout>
);
}
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 Layout component that accepts header and footer content as props.
Input / Output
Output
Render a React component.
Constraints
Use children for main content.
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 Layout({ header, footer, children }) {
return (
<div style={{ fontFamily: 'system-ui, Arial, sans-serif' }}>
<div>{header}</div>
<main style={{ padding: 16 }}>{children}</main>
<div>{footer}</div>
</div>
);
}
export default function App() {
return (
<Layout
header={<header style={{ padding: 16, borderBottom: '1px solid #eee' }}><strong>meetcode</strong></header>}
footer={<footer style={{ padding: 16, borderTop: '1px solid #eee', color: '#555' }}>Keep practicing</footer>}
>
<h1 style={{ marginTop: 0 }}>Topics</h1>
<p>Pick one and start.</p>
</Layout>
);
}
Solutions (0)
No solutions submitted yet. Be the first!