Chap03 -> Creating function-based components -> Optional and default props

Chap03
Jason Zhu 2022-03-26 00:16:30 +11:00
parent 851a26d1b9
commit dafd006124
1 changed files with 13 additions and 1 deletions

View File

@ -3,11 +3,19 @@ import { QuestionData } from './QuestionsData';
interface Props { interface Props {
data: QuestionData; data: QuestionData;
showContent?: boolean;
} }
export const Question = ({ data }: Props) => ( export const Question = ({ data, showContent }: Props) => (
<div> <div>
<div>{data.title}</div> <div>{data.title}</div>
{showContent && (
<div>
{data.content.length > 50
? `${data.content.substring(0, 50)}`
: data.content}
</div>
)}
<div> <div>
{`Ask by ${ {`Ask by ${
data.userName data.userName
@ -15,3 +23,7 @@ export const Question = ({ data }: Props) => (
</div> </div>
</div> </div>
); );
Question.defaultProps = {
showContent: true,
};