#Recommendations for updating localstorage based on search params change

3 messages · Page 1 of 1 (latest)

pallid shoal
#

Probably has been asked before, apologies if so...

Basically I want to write to local storage with the search param's value upon successful navigation.

Let's say I have a <Link to="." search={...} /> i.e. only updates the search params on the current page. What's the recommended way of committing that change to localstorage?

My first thought was to make use of onEnter + onStay . Although preferable from a React perspective, it feels wrong to do the work on the rendered Link's onClick (or similar) prop.

worthy portal
#

what do you mean by that last sentence ?

pallid shoal
#

was alluding to writing to localstorage using onClick in the link i.e.

<Link to="." search={...} onClick={() => { localStorage.setItem(...); }} />

basically referring to the idea of avoiding state syncing and effects where possible. not necessarily applicable in this case upon further reflection because I truly want the writing to localstorage to be a side effect. doing it "eagerly" in the onClick is not desirable in my case because I want to make sure that the navigation change succeeds first.