Home  >  Q&A  >  body text

React with typescript defining useState type as an object with string key/value pairs

In React with typescript, I tried defining the useState type as an object with string key/value pairs. I couldn't find the answer so I thought I'd add it.

I've tried this <{ [key: string]: string }>> and some similar variations, but haven't successfully cracked it yet.

P粉014293738P粉014293738281 days ago404

reply all(1)I'll reply

  • P粉973899567

    P粉9738995672024-02-04 11:23:02

    So this works -> useState<{ [key: string]: string }> :-)

    The best rubber duck ever! At least now it's here for everyone else

    reply
    0
  • Cancelreply