Skip to content

Commit

Permalink
Persist state every time form input changes
Browse files Browse the repository at this point in the history
`react-localstorage` listens for the `beforeunload` event and saves `state` at
that point:
https://github.com/josephfrazier/react-localstorage/blob/830829c25cb13ea83d4d3dfc23ef3dbc200c438a/react-localstorage.js#L60

However, `beforeunload` seems not to fire when the PWA is swiped away:
https://stackoverflow.com/questions/49504561/app-swipe-away-handler-in-pwa

So, we have to save state manually as well.
Now that we're not keeping images/videos, this seems fast enough.

Fixes #6
See STRML/react-localstorage#17
  • Loading branch information
josephfrazier committed May 12, 2018
1 parent 3d823c8 commit 4b22f2d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/routes/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,12 @@ class Home extends React.Component {
const value = target.type === 'checkbox' ? target.checked : target.value;
const { name } = target;

this.setState({
[name]: value,
});
this.setState(
{
[name]: value,
},
() => this.saveStateToLocalStorage(),
);
};

// adapted from https://www.bignerdranch.com/blog/dont-over-react/
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8243,7 +8243,7 @@ react-loadable@^5.4.0:

"react-localstorage@https://github.com/josephfrazier/react-localstorage#higher-ordered-component":
version "1.0.0"
resolved "https://github.com/josephfrazier/react-localstorage#830829c25cb13ea83d4d3dfc23ef3dbc200c438a"
resolved "https://github.com/josephfrazier/react-localstorage#075790da00749a514c801ce8b2458728b0c03716"

react-social-icons@^2.8.1:
version "2.8.1"
Expand Down

0 comments on commit 4b22f2d

Please sign in to comment.