Params not yet available in 'pushState' and 'render' event handlers
See original GitHub issueExpected behavior
When handling a ‘pushState’ event, the resolved state.params
should already be available upon calling the event handler.
Actual behavior
Resolved params are only added to state during render.
Steps to reproduce behavior
To reproduce, click on the link to ‘some page’ in this example app:
var choo = require('choo')
var html = require('choo/html')
var app = choo()
app.route('/', function () {
return html`
<body>
<a href="/somepage">some page</a>
</body>
`
})
app.route('/*', function (state) {
return html`
<body>
page: ${state.params.wildcard} <!-- correctly renders 'somepage' -->
</body>
`
})
app.use(function (state, bus) {
bus.on('pushState', function () {
console.log(state.params.wildcard) // logs undefined
})
bus.on('render', function () {
console.log(state.params.wildcard) // logs undefined
})
})
app.mount('body')
This is a problem for my specific use case, because in the ‘pushState’ (or ‘render’) event handler, I want to fetch some data based on the page param, and fill out the page based on the response.
As an aside, I do want to mention that working with choo v5
has been a real joy so far. You absolutely nailed it with the new stripped-down API for app.use
!
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Next.js: Router.push with state - Stack Overflow
@ggnoredo my guess is you're using req.user in getInitialProps but when routing is done in client side there is no req object. req.user...
Read more >Window: popstate event - Web APIs | MDN
pushState () has been used to add a history entry to the history stack, ... the event handler property onpopstate is also available...
Read more >Modern client-side routing: the Navigation API
While SPAs have been able to bring you this feature via the History API (or ... A "navigate" event listener centralizes handling URL...
Read more >Implementing pushState for twitter.com
UI Component · Manages the decision to pushState URLs by listening for document-wide clicks, and keyboard shortcuts · Broadcasts an event to ...
Read more >Backbone.js
Some basic approaches to rendering views can be found in the Backbone primer. ... Events do not have to be declared before they...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m afraid this is still a problem in the current choo (
6.6.0
), I’ve taken the app generated bynpx create-choo-app
and added the following:to
store.js
index.js
gets one more route:and
main.js
’s html output gets some links so we can get there.The resulting behaviour is: first click: params is empty second click: params.date is from the route we just left
So maybe this isn’t closed after all 😿
As for workarounds, you wrote:
But there no longer seems to be a
.start()
anywhere, and moving the.use()
to the end ofindex.js
makes no difference. My workaround is currently asetTimeout
🙈Should be fixed in
choo@6.7.0
🎉