Chap03 -> Creating function-based component -> Function props (aka how to use render prop)

master
Jason Zhu 2022-03-26 14:24:47 +11:00
parent f833534d19
commit 5d7f817620
2 changed files with 7 additions and 3 deletions

View File

@ -11,6 +11,9 @@ export const HomePage = () => (
<button>Ask a question</button>{' '} <button>Ask a question</button>{' '}
{/* This button component is passed as children of Page */} {/* This button component is passed as children of Page */}
</div> </div>
<QuestionList data={getUnansweredQuestions()} /> <QuestionList
data={getUnansweredQuestions()}
renderItem={(question) => <div>{question.title}</div>}
/>
</Page> </Page>
); );

View File

@ -4,13 +4,14 @@ import { QuestionData } from './QuestionsData';
interface Props { interface Props {
data: QuestionData[]; data: QuestionData[];
renderItem?: (item: QuestionData) => JSX.Element;
} }
export const QuestionList = ({ data }: Props) => ( export const QuestionList = ({ data, renderItem }: Props) => (
<ul> <ul>
{data.map((question) => ( {data.map((question) => (
<li key={question.questionId}> <li key={question.questionId}>
<Question data={question} /> {renderItem ? renderItem(question) : <Question data={question} />}
</li> </li>
))} ))}
</ul> </ul>