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 in Node-js by Nidhi
• 11,580 points
91 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 by kavya

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
9,358 views
0 votes
0 answers

How to install NodeJS LTS on Windows as a local user (without admin rights)

I'm using Windows as a simple user ...READ MORE

Aug 11, 2022 in Node-js by Neha
• 9,020 points
2,072 views
0 votes
1 answer

How to open a popup when a button is clicked?

To open a popup (modal) when a ...READ MORE

answered Dec 31, 2024 in Node-js by Navya
92 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 in Node-js by kavya
69 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 in Node-js by kavya
59 views
0 votes
1 answer
0 votes
1 answer
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