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.

I’m facing a problem where I need to fetch data when I enter different routes

Here’s an example with just a single view that needs to fetch data. In reality I will have multiple views, and model.items will change on different views.

I’m not sure how to achieve this functionality without onRoute hooks.

example

const { h, app } = hyperapp
/** @jsx h */

app({
  model: {
    items: []
  },
  reducers: {
    updateItems: (model, data) => ({
      items: data
    })
  },
  effects: {
    fetch: (model, actions) => {
      setTimeout(function () {
        actions.updateItems([1, 2, 3])
      },0)
    }
  },
  view: (model, actions) => {
    /* I need to asynchronously fetch items only one time when I enter routes.
    If I call an effect here in the view, it will rerender infinitely
    since reducers update the model and cause views to render
    */
    actions.fetch()
    
    console.log('this is logged on every render')
    
    return (
      <div>{model.items}</div>
    )
  } 
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jacobtippcommented, Mar 6, 2017

Everything is cleared up, thanks!

3reactions
FlorianWendelborncommented, Mar 6, 2017

@traducer

app({
	actions: {
		fetch (model, actions) =>
			console.log(`fetching ${model.router.params.id}`)
		,
		customGo: (model, actions, path) => {
			actions.router.go(path)
			actions.fetch()
		}
	},
	subscriptions: [
		(model, actions) => actions.fetch()
	]
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks - Fastify
Application Hooks. onReady; onClose; onRoute; onRegister. Scope; Route level hooks; Using Hooks to Inject Custom Properties; Diagnostics Channel Hooks.
Read more >
onRoute hook doesn't receive the resolved schema #2123
I don't understand: from my point of view, the hook onRoute is fired with the route configuration with the resolved schema.
Read more >
Hooks - Fiber
OnRoute is a hook to execute user functions on each route registeration. Also you can get route properties by route parameter.
Read more >
useHistory hook - React Router: Declarative Routing for React.js
The useLocation hook returns the location object that represents the current URL. You can think about it like a useState that returns a...
Read more >
The Hooks of React Router - CSS-Tricks
React Router 5 embraces the power of hooks and has introduced four different hooks to help with routing. You will find this article...
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