Chap03 -> Creating function-based component -> Function props (aka how to use render prop)
parent
b6fdcbd740
commit
072b7dcf88
|
@ -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>
|
||||||
);
|
);
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue