You’re building a reusable Card component for a blog website. The component needs to display a title and some content, but the content could be a paragraph, a list, or even an image. Here’s the code:
Card
import React from 'react'; type CardProps = { title: string; content: React.ReactNode; }; const Card = (props: CardProps) => ( <div> <h2>{props.title}</h2> <div>{props.content}</div> </div> ); const App = () => ( <div> <Card title="First Post" content={<p>This is a blog post.</p>} /> <Card title="To-Do List" content={<ul><li>Task 1</li></ul>} /> </div> );
What is the benefit of using React.ReactNode as the type for the content prop?
React.ReactNode
content
It allows the content prop to accept any renderable React element, like JSX or components.
It prevents the content prop from being passed as an attribute.
It makes the content prop optional with a default value.
It restricts the content prop to only accept strings.
It ensures the content prop can only be a number or boolean.
Consider this code where a Profile component displays a user’s name and an optional bio:
Profile
type ProfileProps = { name: string; bio?: string; }; const Profile = ({ name, bio = "No bio provided" }) => ( <div> <h1>{name}</h1> <p>{bio}</p> </div> ); const App = () => ( <div> <Profile name="Alice" /> <Profile name="Bob" bio="Loves coding!" /> </div> );
What will be displayed when <Profile name="Alice" /> is rendered?
<Profile name="Alice" />
"Alice" and "No bio provided".
"Alice" and "Loves coding!".
"Alice" and an empty paragraph (<p></p>).
<p></p>
"Alice" with no bio text.
A TypeScript error because bio is missing.
bio
You’re debugging a teammate’s code and notice they tried to modify a prop directly:
type DisplayProps = { count: number; }; const Display = (props: DisplayProps) => { props.count = props.count + 1; // Attempting to modify prop return <div>Count: {props.count}</div>; }; const App = () => <Display count={5} />;
What will happen when this code runs in a development environment?
It will cause a TypeScript compilation error.
The application will crash, the component won’t render at all and logs an error in the console.
It will display "Count: 6" with no issues.
It will display "Count: 5" and throw a runtime error.
It will display "Count: 6" but log a warning about mutating props.
Look at this code using destructuring and a boolean prop:
type UserProps = { username: string; isActive: boolean; }; const User = ({ username, isActive }) => ( <div> {username} - {isActive ? "Online" : "Offline"} </div> ); const App = () => ( <div> <User username="Charlie" isActive /> <User username="Dana" isActive={false} /> </div> );
What will be rendered for <User username="Charlie" isActive />?
<User username="Charlie" isActive />
"Charlie - true"
"Charlie - Online"
"Charlie - Offline"
"Charlie - undefined"
A syntax error because isActive lacks a value.
isActive
Imagine you’re creating a Section component that wraps content and passes props to an inner component:
Section
type SectionProps = { title: string; highlight: boolean; }; const InnerSection = ({ title, highlight }) => ( <h1 style={{ color: highlight ? "blue" : "black" }}>{title}</h1> ); const Section = (props) => ( <div> <InnerSection {...props} /> <p>Some content here</p> </div> ); const App = () => <Section title="Welcome" highlight />;
What does the {...props} syntax do in this code?
{...props}
It causes a runtime error because props can’t be spread.
It merges Section’s props with default values in InnerSection.
InnerSection
It copies only the title prop to InnerSection.
title
It passes all props from Section to InnerSection as individual attributes.
It prevents InnerSection from accessing the highlight prop.
highlight
This content is only available to registered users.Please register at the the home page.