I want to get parameters from URL but when I use useParams() it doesn’t work in redux toolkit, please help to solve it?
here is an example of the code:
export const getArticleTitle = createAsyncThunk<ArticleData[], void, { rejectValue: AxiosError }>("article/fetchAllArticle", async (_, { rejectWithValue }) => {
try {
let { title } = useParams();
const newsid = title!.split("-").join(" ");
const response = await artikelService.getArtikelIni(newsid);
return response as ArticleData[];
} catch (error) {
if (error instanceof AxiosError) {
return rejectWithValue(error);
}
throw error;
}
});
I’m hoping to find a way to get parameter from URLs in a way that is suitable for use in the Redux toolkit.
2
Answers
I am assuming, you have
url
like this:https://example.com/news/ABC-123-ABC
It will return the
text
after the last index of/
likeuseParams
Use
URLSearchParams
API to get Query parameters like?title=abc
Use JS native search params instaed of react hook