-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(v2): use base url to navigate to search page #2838
Conversation
Add baseUrl to search page query URL.
Deploy preview for docusaurus-2 ready! Built with commit 22985fb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This fix is in the right direction, but we should just use the useBaseUrl()
hook in @docusaurus/useBaseUrl
@yangshun thanks for your review! |
I found a side effect and it might be caused by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tetunori due to the Rules of hooks, in this case it will not be possible to use useBaseUrl
hook. Therefore, your original solution was correct.
|
||
const SEARCH_PARAM_QUERY = 'q'; | ||
|
||
function useSearchQuery() { | ||
const history = useHistory(); | ||
const location = useLocation(); | ||
const baseUrl = useBaseUrl(''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use it as such -
const newPath = useBaseUrl(`search?q=${searchValue}`);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don’t call Hooks inside loops, conditions, or nested functions
Would that work? I do not think so. Therefore siteConfig.baseUrl
is a good option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a hook, why would it not work? How would useHistory()
and useLocation()
work then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try it yourself and you will see that it does not work.
navigateToSearchPage: (searchValue) => {
const searchUrl = useBaseUrl(`search?q=${searchValue}`); // Not work - call hook in nested functions.
history.push(searchUrl);
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we put it at the top level of the function scope, alongside useLocation()
and useHistory()
, and not in the navigateToSearchPage()
function, would that not work? That is my suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this way also will not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try it yourself and you will see that it does not work.
navigateToSearchPage: (searchValue) => { const searchUrl = useBaseUrl(`search?q=${searchValue}`); // Not work - call hook in nested functions. history.push(searchUrl); },
I confirmed this did not work because of the error "Invalid hook call".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we put it at the top level of the function scope, alongside
useLocation()
anduseHistory()
, and not in thenavigateToSearchPage()
function, would that not work? That is my suggestion.
Thanks, but in the top level of the function scope, we cannot get searchValue
defined in navigateToSearchPage()
. So your suggestion would not work, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I failed to realize we need searchValue
. Hmm I guess this is fine then. Mind leaving a comment in the code and why we're doing this? Could add a link to this PR in the comment to give more context :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reverts commit 72b319a.
Added link to this PR and did following tests.
|
@tetunori thank you! |
Motivation
To resolve #2827.
Have you read the Contributing Guidelines on pull requests?
Yes.
Test Plan
yarn prettier && yarn lint
).yarn test
)