question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State lost when changing window.location.pathname

See original GitHub issue

Expected behavior

The state to be preserved

Actual behavior

State is lost if window.location.pathname is changed

Steps to reproduce behavior

I have this set up

// my main js file
app.router(route => [
  route('/', mainView),
  route('/game', gameView)
])
/*
 * In my mainView I have a component like this
 */
const configPlayer = (state, send) => {
  return html`
  <div class="config-panel">
    <input id="player" type="text">
    <button onclick=${e => send('player:join')}>Play</button>
  </div>
  `
}
// the model for the view
module.exports = {
  namespace: 'player',
  state: {
    name: '',
    id: ''
  },
  reducers: {
    /**
     * set initial data
     */
    init: (data, state) => {
      console.log(JSON.stringify(data, null, 2))
      return {
        name: data.player,
        id: '1'
      }
    }
  },
  effects: {
    join: (data, state, send, done) => {
      send('player:init',
        { player: document.getElementById('player').value },
        done)
      setTimeout(function () {
        console.log(JSON.stringify(state, null, 2))
        window.location.pathname = '/game'
      }, 2000)
    }
  }
}
// and finally the called view
const gameView = (state, prev, send) => {
  console.log(JSON.stringify(prev, null, 2))
  return html`
    <div class="container">
      <h1>Hello ${state.player.name}!</h1>
      <div class="board">
      </div>
    </div>
  `
}

As you can see, clicking the button trigger the join effect, that trigger the init reducer and then change the pathname. The init and join consoles log prints data correctly modified, but the one in the gameView prints the initial state if I log the state and an empty object if I print prev. I’m not sure why the state is lost, but I think it wouldn’t happen if I make the redirect with choo router, I just don’t know how to access the router from a reducer or effect. I have seen examples using anchor tags but not changing the path from a model hook. How can I access the router from effects/reducers? Is there a better way to navigate thru routes from a choo model?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
YerkoPalmacommented, Jul 15, 2016

Thanks for the answer. I just manage to programatic navigate through my routes, thanks to this issue. I had to use this signature

send('location:setLocation', { location: '/game' }, done)
window.history.pushState({}, null, '/game')

'app:location didn’t work, as the issue say, also send parameters are different because of the version of choo referred in the issue. Thanks for your feedback, I’ll close the issue.

1reaction
yoshuawuytscommented, Jul 13, 2016

From my phone: if I’m not mistaken changing window.location.pathname causes a full page reload which indeed loses state - there’s an open issue for changing the state location, with a workaround included until this is solved (which should be soon; its a priority) - hope this answers your question! - cheers

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to restore state while changing window.location.href
On successful authentication from the server, I call window.location.href = 'newURL'. This call reloads my app. Now the user info I stored to ......
Read more >
location.replace() - Web APIs - MDN Web Docs
The replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the ...
Read more >
window.location Cheatsheet | SamanthaMing.com
The window.location object can be used to get information on the current page address (URL). You can also use its method to do...
Read more >
Difference between window.location.href ... - GeeksforGeeks
Window.location is a property that returns a Location object with information about the document's current location. This Location object ...
Read more >
How do I use JavaScript to modify the URL without reloading ...
Learn all of the options JavaScript provides for modifying the URL of the current page in the browser without reloading the page.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found