Issue
How do I convert an Enum to a Constant Class like this, in Typescript? Currently using React Typescript, with functional components . This question is slightly different: TypeScript enum to object array
export enum ProductStatus {
Draft = 1,
Review = 2,
Approved = 3,
Rejected = 4,
Billed = 5,
Collected = 6,
Unpayable = 7,
WorkInProgress = 8,
ReadyToReview = 9,
NeedsRevision = 10,
Failed = 11,
}
export const ProductStatus = {
Draft: 1,
Review: 2,
Approved: 3,
Rejected: 4,
Billed: 5,
Collected: 6,
Unpayable: 7,
WorkInProgress: 8,
ReadyToReview: 9,
NeedsRevision: 10,
Failed: 11,
};
Solution
loop throught the keys and use ethe reduce
function
const newConst = React.useMemo(() => {
return Object.keys(ProductStatus).reduce((acc, key) => {
if (isNaN(Number(key))) {
acc[key] = ProductStatus[key];
}
return acc;
}, {});
}, []);
console.log(newConst );
https://stackblitz.com/edit/react-ts-ivfxqw?file=App.tsx
Answered By - Sachila Ranawaka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.