Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript array of function components

Why I could not do:
<BookingGetServices numGuests={this.state.numGuests}/>

Here is the code:

constructor()
    {
        super({});

        this.pages = [
            <BookingGetNumGuests submitNumGuests={this.submitNumGuests}/>,
            <BookingGetServices numGuests={this.state.numGuests}/>
        ];

        this.state = {
            pageIndex: 0,
            numGuests: -1
        };
    }
interface DefinedProps
{
    numGuests: number
}

function BookingGetServices(props: DefinedProps)
{
    const {numGuests} = props;

    return (
        <header className={"BookingPage-header"}>
            <h1>{numGuests}</h1>
        </header>
    );
}
BookingApplication.tsx:26 
        
       Uncaught TypeError: Cannot read properties of undefined (reading 'numGuests')
    at new BookingApplication

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are trying to access this.state before it exists

this.state = {
  pageIndex: 0,
  numGuests: -1
};

this.pages = [
  <BookingGetNumGuests submitNumGuests={this.submitNumGuests}/>,
  <BookingGetServices numGuests={this.state.numGuests}/>
];

The fact that it’s TypeScript doesn’t change anything, because this is a runtime error

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading