Don't use custom history API (#485)

* Add shallowEquals check for onUpdate

* Using Next.js Router instead of custom history API

Fixes https://github.com/zeit/next.js/issues/4994

* Remove urlObject code
main
Tim Neutkens 6 years ago committed by Michael Fix
parent d41f05d9c1
commit 2e945fc528

@ -6,6 +6,7 @@ import { DragDropContext } from 'react-dnd'
import domtoimage from 'dom-to-image' import domtoimage from 'dom-to-image'
import ReadFileDropContainer, { DATA_URL, TEXT } from 'dropperx' import ReadFileDropContainer, { DATA_URL, TEXT } from 'dropperx'
import Spinner from 'react-spinner' import Spinner from 'react-spinner'
import shallowEquals from '../lib/shallow-equals'
// Ours // Ours
import Button from './Button' import Button from './Button'
@ -113,8 +114,10 @@ class Editor extends React.Component {
window.removeEventListener('online', this.setOnline) window.removeEventListener('online', this.setOnline)
} }
componentDidUpdate() { componentDidUpdate(prevProps, prevState) {
this.props.onUpdate(this.state) if(!shallowEquals(this.state, prevState)) {
this.props.onUpdate(this.state)
}
} }
getCarbonImage({ format, type } = { format: 'png' }) { getCarbonImage({ format, type } = { format: 'png' }) {

@ -1,12 +1,5 @@
import Morph from 'morphmorph' import Morph from 'morphmorph'
let createHistory
let history
if (typeof window !== 'undefined') {
createHistory = require('history').createBrowserHistory
history = createHistory()
}
const mapper = new Morph({ const mapper = new Morph({
types: { types: {
bool: v => { bool: v => {
@ -68,11 +61,6 @@ export const deserializeState = serializedState => {
return JSON.parse(decodeURIComponent(stateString)) return JSON.parse(decodeURIComponent(stateString))
} }
const keysToQuery = keys =>
`?${Object.keys(keys)
.map(key => `${key}=${keys[key]}`)
.join('&')}`
export const getQueryStringState = query => { export const getQueryStringState = query => {
if (query.state) { if (query.state) {
return deserializeState(query.state) return deserializeState(query.state)
@ -88,15 +76,16 @@ export const getQueryStringState = query => {
return state return state
} }
export const updateQueryString = state => { export const updateQueryString = (router, state) => {
if (history.location.search.indexOf('react_perf') < 0) { const mappedState = mapper.map(reverseMappings, state)
const mappedState = mapper.map(reverseMappings, state) serializeCode(mappedState)
serializeCode(mappedState)
history.replace({ router.replace({
search: encodeURI(keysToQuery(mappedState)) pathname: router.pathname
}) }, {
} pathname: router.pathname,
query: mappedState
}, {shallow: true})
} }
// private // private

@ -0,0 +1,25 @@
export default function shallowEquals (a, b) {
for (const i in a) {
// NaN === NaN is false in Javascript
if(isNaN(b[i]) && isNaN(a[i])) {
continue
}
if (b[i] !== a[i]) {
console.log('DID NOT MATCH', i, a[i], b[i])
return false
}
}
for (const i in b) {
// NaN === NaN is false in Javascript
if(isNaN(b[i]) && isNaN(a[i])) {
continue
}
if (b[i] !== a[i]) {
return false
}
}
return true
}

@ -11,16 +11,17 @@ import { saveState } from '../lib/util'
class Index extends React.Component { class Index extends React.Component {
render() { render() {
const {router} = this.props
return ( return (
<Page enableHeroText={true}> <Page enableHeroText={true}>
<Editor {...this.props.router} onUpdate={onEditorUpdate} api={api} /> <Editor {...router} onUpdate={(state) => onEditorUpdate(router, state)} api={api} />
</Page> </Page>
) )
} }
} }
function onEditorUpdate(state) { function onEditorUpdate(router, state) {
updateQueryString(state) updateQueryString(router, state)
const s = { ...state } const s = { ...state }
delete s.code delete s.code
delete s.backgroundImage delete s.backgroundImage

Loading…
Cancel
Save