-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: Reload entries by clicking on active menu item
This is quite hacky – router does not re-render the route when clicking on an already active link because its data is based on the document.location. To bypass this, we need to pass an extra value in the location’s state object that will let us know we should reload. And for similar reasons, it cannot just be a boolean (or we would have to clear the state each time to be able to tell when the value changes again) so we are back at increasing counters. Unfortunately, that is not end of it since any navigation would clear the state, triggering another reload so we also need to use another counter that only increases when the counter in location state increases. Fixes: #1287
- Loading branch information
Showing
6 changed files
with
109 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useState } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
|
||
/** | ||
* Changes its return value whenever the value of forceReload field | ||
* in the location state increases. | ||
*/ | ||
export function useShouldReload() { | ||
const location = useLocation(); | ||
const forceReload = location?.state?.forceReload; | ||
const [oldForceReload, setOldForceReload] = useState(forceReload); | ||
|
||
if (oldForceReload !== forceReload) { | ||
setOldForceReload(forceReload); | ||
} | ||
|
||
// The location state is not persisted during navigation | ||
// so forceReload would change to undefined on successive navigation, | ||
// triggering an unwanter reload. | ||
// We use a separate counter to prevent that. | ||
const [reloadCounter, setReloadCounter] = useState(0); | ||
if (forceReload !== undefined && forceReload !== oldForceReload) { | ||
let newReloadCounter = reloadCounter + 1; | ||
|
||
setReloadCounter(newReloadCounter); | ||
return newReloadCounter; | ||
} | ||
|
||
return reloadCounter; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters