I have a list of entities and a few filters (date range, name etc.) to fetch those entities. On each entity I have a link redirecting to the details of this entity.
I would like to
- persist state of filters when user comes back from the entity details page
- reset filters state to default if user navigates from other page, refreshes page etc.
I tried storing filters in localStorage but I can't find any reliable way to detect that the back button was clicked on the entities details page. Any ideas how can I achieve this?
Simplified code:
function Sample() {
const [filters, setFilters] = React.useState();
const [entities, setEntities] = React.useState();
const history = useHistory();
/***
*
* Entities fetching etc....
*
*/
const handleNavigateToEntity = (entity) => {
history.push(`/entities/${entity.id}`)
}
return (
<div>
<div>...SOME FILTERS...</div>
<ol>
{entities.map(e => <li><button onClick={handleNavigateToEntity}></button></li>)}
</ol>
</div>
)
}
What I want to achieve:
if(navigatedBackToThisPage){
const persistedFilters = readFiltersFromStorage();
setFilters(persistedFilters);
}