I am new to react typescript. I am searching for hours and no solution.
I have a json object saved inside of file categories.ts like
const categories = [
{value: "Farsi", label: "fa"},
{value: "English", label: "en"},
{value: "Hindi", label: "hi"},
{value: "Spanish", label: "es"},
{value: "French", label: "fr"},
{value: "Japanese", label: "ja"},
{value: "Russian", label: "ru"},
{value: "German", label: "de"},
{value: "Italian", label: "it"},
{value: "Korean", label: "ko"},
{value: "Brazilian Portuguese", label: "pt-BR"},
{value: "Arabic", label: "ar"},
{value: "Turkish", label: "tr"}
];
export default categories;
This is the code I am working which is saved inside a utility file called useDictionary.ts
import { useState, useEffect } from "react";
import categories from "../data/categories";
type dataCategoryType = {
value: string;
label: string;
}
const useDictionary = () => {
const [category, setCategory] = useState<dataCategoryType[]>([]);
// setCategory(<dataCategoryType[]> JSON.parse(categories));
const cat: DataCategoryType = <dataCategoryType[]> JSON.parse(categories);
setCategory(cat);
return {
category
};
export default useDictionary;
the error is
Argument of type '{ value: string; label: string; }[]' is not assignable to parameter of type 'string'.ts(2345)
(alias) const categories: {
value: string;
label: string;
}[]
import categories
can you please tell me the proper way of seasoned typescript folks do this?
Please also note the returned value category is passed to APP component. like
const { category } = useDictionary();
Many thanks in advance,
2
Answers
I had to make my comment to be an answer.
The first thing wrong with your code is that you are treating categories as a JSON whereas it is an Object (javascript ARRAY), so, JSON.parse would keep throwing an error, and get rid of the parse, meaning the code needs to look like this.
Now we’ve fixed that, if you are going to set the category state (that you already have before hand), you can do that directly on the
useState
initialization.and get rid of the cat variable and the setCategory beneath that, final code should look like this.
Issue:
Updated Code:
You can also provide a return type for useDictionary hook. Normally we also return the setCategory here from the hook so that we can update the categories from outside of this hook. But it depends on your use case also.
Let me know if it helps.