Answer the question
In order to leave comments, you need to log in
How to use useLocation hook in storybook?
Tell me how to integrate the useLocation hook into the storybook component?
For example, there is a story:
export default {
component: Header,
title: 'Header'
};
export const Default = (args) => <Header {...args} />;
const usePathname = () => {
const location = useLocation();
return location.pathname;
};
TypeError
Cannot read properties of undefined (reading 'location')
Answer the question
In order to leave comments, you need to log in
useLocation works if the component with it is directly or indirectly inside the Router.
As you know, Router receives a history instance in props, puts it in the context, and useLocation takes it out of the context.
Perhaps in the storybook you forgot to wrap everything in a Router
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question