I have been trying to use a map to store the responses of proxies from two different providers to make it easier to look them up as they would be stored in a hash-map. For example:
let proxies = new Map<String, Pod[]>();
proxies.get("my-provider");
However when I try to render the data to the page using a react useState hook with typescript, apparently the types differ. My type definition is as follows:
export interface Pod {
pod: number;
timestamp: string; <- ISO UTC timestamp
status: string; <-- 200 OK, 4xx, 5xx etc.
}
export interface PodProvider {
provider: {
[key: string]: Pod[]; <- indexed map containing a list
}
}
I have a useEffect hook that calls a method to probe the proxies when the component has loaded and then adds the responses to the hashmap along with a unique key to identify the provider of said proxies as follows:
const [response, setResponse] = useState<Map<String, Pod[]>>();
useEffect(() => {
const goProbe = async () => {
const results = await probe(provider1Proxies);
proxies.set("provider1", results);
setResponse(proxies);
}
goProbe();
}, []);
Lastly, the logic for how to render the state to the DOM is provided in a separate component like this:
export const Logger: React.FC<PodProvider> = ({ provider }, identifier: string) => ( <- notice the type used here
<React.Fragment>
<Table size="small">
<Caption>
<XL>Log Output</XL>
</Caption>
<Head>
<HeaderRow>
<HeaderCell>Timestamp</HeaderCell>
<HeaderCell>Pod</HeaderCell>
<HeaderCell>Status</HeaderCell>
</HeaderRow>
</Head>
<Body>
{
provider[identifier].flatMap((response, key) => {
return (
<Row key={key}>
<Cell>{response.pod}</Cell>
<Cell>{response.status}</Cell>
<Cell>{response.timestamp}</Cell>
</Row>
);
})
}
</Body>
</Table>
</React.Fragment>
);
My question is what did I do wrong? Why are Map<String, Pod[]> and '{ [key: string]: Pod[]; } different? How to fix the code to do as expected?
Thanks :)