Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

generics functional component

import * as React from 'react';
import { render } from 'react-dom';

interface IProps<T> {
    collapsed: boolean;
    listOfData: T[];
    displayData: (data: T, index: number) => React.ReactNode;
}
const CollapsableDataList = <T extends object>(props: IProps<T> & { children?: ReactNode }) => {
    if (!props.collapsed) {
        return <span>total: {props.listOfData.length}</span>
    } else {
        return (
            <>
                {
                    props.listOfData.map(props.displayData)
                }
            </>
        )
    }
}


render(
    <CollapsableDataList
        collapsed={false}
        listOfData={[{a: 1, b: 2}, {a: 3, c: 4}]}
        displayData={(data, index) => (<span key={index}>{data.a + (data.b || 0)}</span>)}
    />,
    document.getElementById('root'),
)
Comment

PREVIOUS NEXT
Code Example
Typescript :: react table typescript 
Typescript :: Update Object Value in Ts/JS 
Typescript :: get random dark color 
Typescript :: simple input for games javascript 
Typescript :: footer credits with jquery date time 
Typescript :: how can i take multiple inputs from the user in discord.js 
Typescript :: typescript compile on save 
Typescript :: what are the common mistakes in testing 
Typescript :: laravel many to many get related posts by category 
Typescript :: get all elements with id starts and class 
Typescript :: accessing list elements in dictionary python 
Typescript :: array of objects value repeat check 
Typescript :: typescript type number range 
Typescript :: rails_env production rake assets precompile 
Typescript :: typescript check if object is of type 
Typescript :: mixpanel for typescript 
Typescript :: typescript null and undefined check 
Typescript :: Lists - Learn C# 
Typescript :: angular initail valeur in fromgroup 
Typescript :: pyton program acept user first and last name and prints in revese 
Typescript :: global declaration css ts 
Typescript :: in grunt cannot be loaded because running scripts is disabled on this system 
Typescript :: where to create assets folder in flutter 
Typescript :: format time to am pm 
Typescript :: gettime is not a function typescript 
Typescript :: order documents in firestore 
Typescript :: react components for login 
Typescript :: Electron WebContents context-menu 
Typescript :: nest js get request response 
Typescript :: type in typescript 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =