Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

reactjs - how to dynamically build this table in react and typescript?

I have the following React/Typescript code that I'm using to build a row which gets passed into a framework table component:

const row = ({ record }: RowProps) => <tr>
{
    <React.Fragment>
        <td key={'UserName'}>
            {record.UserName}
        </td>
        <td key={'UserEmail'}>
            {record.UserEmail} 
        </td>
    </React.Fragment>
}
</tr>

How can I make this generic using React/TypeScript? The example above uses the following RowProps definition for a User component:

interface RowProps {
    record: {
        UserName: string,
        UserEmail: string
    }
}

But let's say that I had a separate Product component with the following structure:

interface RowProps {
    record: {
        ProductId: number,
        ProductName: string,
        ProductDescription: string
    }
}

I'm looking for a clean, programmatic, dynamic way to generate the appropriate TD's based on the RowProps passed in. So in my second example above, the generic builder implementation would write out 3 TD's instead of 2, based on the extra record property.

question from:https://stackoverflow.com/questions/65944775/how-to-dynamically-build-this-table-in-react-and-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...