Can't get Reflect.metadata to work in React

426 views Asked by At

I have this Typescript code:

import 'reflect-metadata';

export const formatMetadataKey = Symbol("format");

export function format(formatString: string)
{
  return Reflect.metadata(formatMetadataKey, formatString);
}

export function getFormat(target: any, propertyKey: string)
{
  return Reflect.getMetadata(formatMetadataKey, target, propertyKey);
}

class MyData
{
  @format("dd.MM.yyyy")
  dateString: string = "";
}

Later in the code, in another file, I have a ReactJs component that takes an instance of MyData

const MyComponent = (props) =>
{
   const { data } = props;

   // trying to see if a property has a certain decorator here
   const meta = getFormat(data, "dateString");

   ...
}

Tried a gazillion different things, but getFormat always outputs undefined instead of the metadata.

0

There are 0 answers