Chap03 -> Creating function-based component -> Children props

Chap03
Yiqing Zhu 2022-03-26 14:10:10 +11:00
parent dafd006124
commit 4affa35f00
3 changed files with 25 additions and 3 deletions

View File

@ -1,13 +1,15 @@
import React from 'react';
import { PageTitle } from './PageTitle';
import { Page } from './Page';
import { QuestionList } from './QuestionList';
import { getUnansweredQuestions } from './QuestionsData';
export const HomePage = () => (
<div>
<Page>
<div>
<h2>Unanswered Questions</h2>
<PageTitle>Unanswered Questions</PageTitle>
<button>Ask a question</button>
</div>
<QuestionList data={getUnansweredQuestions()} />
</div>
</Page>
);

View File

@ -0,0 +1,15 @@
import React from 'react';
import { PageTitle } from './PageTitle';
interface Props {
title?: string;
children: React.ReactNode;
}
export const Page = ({ title, children }: Props) => (
<div>
{title && <PageTitle>{title}</PageTitle>}
{children}
{/* Component nested within Page component will be rendere here */}
</div>
);

View File

@ -0,0 +1,5 @@
import React from 'react';
interface Props {
children: React.ReactNode;
}
export const PageTitle = ({ children }: Props) => <h2>{children}</h2>;