Created
February 6, 2018 05:49
-
-
Save banhaclong20/a097ffda1c9fdb2009a356cd6dad9e85 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import serializeForm from 'form-serialize' | |
const getLocalStorage = (key) => { | |
let data = null | |
try { data = JSON.parse(localStorage[key]) } catch() {} | |
return data | |
} | |
const setLocalStorage = (key, data) => { | |
localStorage[key] = JSON.stringify(data) | |
} | |
class Form extends React.Component { | |
constructor({ route }, { router }) { | |
super() | |
this.state = { formData: getLocalStorage('whatever') || {} } | |
router.setRouteLeaveHook(route, () => { | |
setLocalStorage('whatever', serializeForm(findDOMNode(this))) | |
}) | |
} | |
} | |
Form.contextTypes = { router: React.PropTypes.object } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment