How is a single TypeScript Interface object passed as a prop to a React child component

0 votes
With the help of code examples can you tell me How is a single TypeScript Interface object passed as a prop to a React child component?
Feb 12, 2025 in Node-js by Nidhi
• 16,260 points
562 views

1 answer to this question.

0 votes

You can pass a single object that conforms to a specific interface as a prop to a child component. This approach allows you to encapsulate multiple related properties into a single prop, enhancing code readability and maintainability.

Defining the Interface:

// types.ts

export interface User {

  name: string;

  age: number;

  email: string;

}

Parent Component:

// ParentComponent.tsx

import React from 'react';

import { User } from './types';

import ChildComponent from './ChildComponent';

const ParentComponent: React.FC = () => {

  const user: User = {

    name: 'John Doe',

    age: 30,

    email: 'john.doe@example.com',

  };

  return <ChildComponent user={user} />;

};

export default ParentComponent;

Child Component:

// ChildComponent.tsx

import React from 'react';

import { User } from './types';

interface ChildComponentProps {

  user: User;

}

const ChildComponent: React.FC<ChildComponentProps> = ({ user }) => {

  return (

    <div>

      <h1>{user.name}</h1>

      <p>Age: {user.age}</p>

      <p>Email: {user.email}</p>

    </div>

  );

};

export default ChildComponent;
answered Feb 21, 2025 by kavya

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How do I use pointer-events to react only to scroll events, or is there a better approach?

Capturing Only Scroll Events in React If you ...READ MORE

answered Feb 22, 2025 in Node-js by Kavya
603 views
0 votes
1 answer

How to use the render function to display a React component?

To display a React component, use the ...READ MORE

answered Mar 21, 2025 in Node-js by Anvi
523 views
0 votes
1 answer

How to manage state within a React component?

In React, state is managed differently in ...READ MORE

answered Mar 26, 2025 in Node-js by anonymous
637 views
0 votes
1 answer

What distinguishes the loadingIndicatorSource prop from the defaultSource prop in React Native?

Property loadingIndicatorSource defaultSource Purpose Placeholder during remote image loading. Placeholder before load ...READ MORE

answered Feb 21, 2025 in Node-js by kavya
545 views
0 votes
1 answer

How are default props declared in a React functional component?

In React functional components, default prop values ...READ MORE

answered Feb 21, 2025 in Node-js by kavya
648 views
0 votes
1 answer
0 votes
1 answer

How can a:hover be implemented using inline CSS styles in React?

In React, inline styles do not support ...READ MORE

answered Feb 21, 2025 in CSS by kavya
1,282 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP