Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Router - add a hook for initial state #102

Closed
codingismy11to7 opened this issue Apr 13, 2015 · 7 comments
Closed

Router - add a hook for initial state #102

codingismy11to7 opened this issue Apr 13, 2015 · 7 comments
Labels
Milestone

Comments

@codingismy11to7
Copy link

It'd be nice to have a way to hook into the Router so that the initial location can be hooked (like the onRouteChange hook), or having onRouteChange called for the initial load. Not super important, just setting the window title in my case, but don't want to have client-side code changing titles with RoutingRules.Loc matches during onRouteChange and then a different piece of code on the server operating on /path strings just for the initial page load.

@ramnivas
Copy link
Contributor

+1 I recently faced a situation, where this would have helped. In my case, I need to do some page-level initialization (non-React) upon rendering any page--including the initial page.

Either of your suggestion would work fine for my use case. I guess calling onRouteChange for the initial load could be a bit controversial, since that will be akin to calling an observer even for the initial setting of values in an observable.

Alternatively, since Router is a React component, I wonder if there is a way for users to specify componentDidMount (I looked through the source code but got a bit lost). I believe that will also help with your and my use case.

@codingismy11to7
Copy link
Author

@ramnivas smart - this works fine for me:

  val routerComponent = {
    Router.componentUnbuilt(router)
      .componentDidMount(scope  setWindowTitle(scope.state))
      .buildU
  }

@ramnivas
Copy link
Contributor

@codingismy11to7 Cool! I will try the same shortly.

@codingismy11to7
Copy link
Author

@japgolly if that's supported (and will remain supported), maybe a line in the router docs pointing that out would be better than some new method of hooking into the initial state

@japgolly
Copy link
Owner

japgolly commented Apr 13, 2015 via email

@japgolly japgolly added the maybe label Apr 22, 2015
@ngbinh
Copy link

ngbinh commented Apr 22, 2015

It works but feel hacky to me as we have to unbuilt then build the router.

@japgolly japgolly modified the milestone: 0.9.0 Apr 23, 2015
@japgolly japgolly removed the maybe label May 9, 2015
japgolly added a commit that referenced this issue May 17, 2015
New router that:
* uses an ADT to represent pages
* allows rules to be composed and then modified collectively
* clarifies the difference between Router component and controller
* accomodates stateful logic

Addresses #96, #103, #102, #94
japgolly added a commit that referenced this issue May 17, 2015
New router that:
* uses an ADT to represent pages
* allows rules to be composed and then modified collectively
* clarifies the difference between Router component and controller
* accomodates stateful logic

Addresses #96, #103, #102, #94
@japgolly
Copy link
Owner

Closed via 93bbc5f.

There's a new Router coming in 0.9 that fixes this.

In RouterConfig there are {set,on}PostRender methods (see here). The callback receives (Option[Page], Page) and for this case of an initial state hook, the Option[Page] will be None.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants