I am trying to return useRouter() variable inside useEffect().But unable to get it. getting undefined error inside the console. I actually fetching the data from API on the basis of the slug.
import { useRouter } from 'next/router';
import {useEffect, useState} from 'react';
import axios from "axios";
const Post = () => {
const router = useRouter();
const [snippets,SetSnippets] = useState();
const { snippet_slug } = router.query;
var config = {
headers: {
accept: '*/*',
'Content-Type': 'application/json',
'API_ACCESS_KEY': 'hns2V0Ddbkkn8r1XLq3Kw7ZoiBTR0nmA',
}
};
const url = 'api/viewsnippets';
useEffect(async ()=>{
var data = {
slug: snippet_slug,
}
console.log(snippet_slug); // Here i am getting error(i.e undefined)
await axios.post(url,data,config)
.then(function (response) {
SetSnippets(response.data);
});
},[]);
return (
<>
{snippets && (
<h1>{snippets.snippet_title}</h1>
)}
</>
);
}
export default Post;