I have an dynamic route which is sitename.com/[username]
When I navigate from sitename.com/account to sitename.com/[username] (for example when I am in /account page, I click an username which is abdulkadirkg) it is working. But if I am in sitename.com/abdulkadirkg and I click another username which is sitename.com/otherusername although the link has changed it doesn't work. My code is here;
<NextLink href={{pathname:"/[username]",query:{username:subscriber.userName}}} >
<Typography variant="subtitle2" noWrap>
{firstName} {lastName}
</Typography>
</NextLink>
I also tried this;
<NextLink href={PATH+subscriber.userName} >
Full Code ([username].js);
export default function UserProfile({username}) {
const router = useRouter();
const [user, setUser] = useState({});
const [isSameUser,setIsSameUser] = useState(false);
if (username === undefined) username = router.query.username;
const authenticatedUser = useAuth();
useEffect(() => {
async function fetchData() {
await axios.get("/User/GetProfileByUserName/" + username).then(response => {
setUser(response.data.data);
}).catch(err => enqueueSnackbar(err.message, {variant: 'error'}))
}
fetchData();
setIsSameUser(username === authenticatedUser.user.userName);
}, [])
return (
<Page title="User: Profile">
<Container maxWidth={themeStretch ? false : 'lg'}>
<HeaderBreadcrumbs
heading="Profile"
/>
<Card>
<ProfileCover user={user}/>
</Card>
</Container>
</Page>
);
}
in ProfileSubscribers.js;
<NextLink href={PATH+subscriber.userName} >
useEffecthook to "listen" for changes on the dynamic part of your route path so it can handle the change or whatever it needs to do with the new value.useEffecthook is for, issuing side-effects as a response to some external change.useEffecthook with appropriate dependency is roughly equivalent tocomponentDidUpdatelifecycle method from class components. Perhaps it would be helpful to share an example for what needs to "respond" to this dynamic route changing.usernameis obviously, and should be, a dependency for theuseEffecthook fetching data.