Skip to content

Instantly share code, notes, and snippets.

@banhaclong20
Created February 6, 2018 05:49
Show Gist options
  • Save banhaclong20/a097ffda1c9fdb2009a356cd6dad9e85 to your computer and use it in GitHub Desktop.
Save banhaclong20/a097ffda1c9fdb2009a356cd6dad9e85 to your computer and use it in GitHub Desktop.
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