Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Route.open does not fire on initial page load if route is matched #4

Closed
kirillku opened this issue Jun 30, 2021 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@kirillku
Copy link
Owner

Currenly Route.open will not fire on initial page load even if route is matched. So, Route.open is not reliable at the moment.

This should be possible to achieve

const FriendsRoute = createRoute('/friends')

// Should also load friends if even if user initally opened "/friends" url
// Currenlty will only work when user opened some other url at first and then navigated to "/friends"
sample({
  sourse: FriendsRoute.open,
  target: loadFriendsFx
})

Simplified implementation details (effector playground)

const $pathname = createStore('/')

const open = createEvent()
const close = createEvent()

split({
  source: $pathname,
  match: store => (store ? 'open' : 'close'),
  cases: {open, close},
})

const Route = {open, close}

// Only the code above could be changed. The code below should stay as is.

const fx = createEffect(pathname =>
  console.log(`I should fire with pathname "${pathname}"`)
)

sample({
  source: Route.open,
  target: fx,
})

Locations in code:

@kirillku
Copy link
Owner Author

kirillku commented Jun 30, 2021

Adding watch does not help. Route.open is firing now, but it happens before it used, so does not make any difference.

const $pathname = createStore('/')

const open = createEvent()
const close = createEvent()

const pathnameChanged = createEvent()

split({
  source: pathnameChanged,
  match: store => (store ? 'open' : 'close'),
  cases: {open, close},
})

$pathname.watch(pathnameChanged)
// `Route.open` will fire now.

const Route = {open, close}

// Only the code above could be changed. The code below should stay as is.

const fx = createEffect(pathname =>
  console.log(`I should fire with pathname "${pathname}"`)
)

// sample will still not fire as `Route.open` fired before sample was created.
sample({
  source: Route.open,
  target: fx,
})

@kirillku
Copy link
Owner Author

The only solution I see at the moment is to remove Route.open and Route.close and always use Route.match

const FriendsRoute = createRoute('/friends')

guard({
  sourse: FriendsRoute.match,
  filter: FriendsRoute.status,
  target: loadFriendsFx
})

(Worth changing Route.status to Route.matched or Route.isOpen)

@kirillku
Copy link
Owner Author

kirillku commented Jul 7, 2021

Removing Route.open and Route.close does not really help, as guards are not triggered

const FriendsRoute = createRoute('/friends')

// Will not trigger on initial page load
guard({
  sourse: FriendsRoute.match,
  filter: FriendsRoute.status,
  target: loadFriendsFx
})

@kirillku kirillku added the bug Something isn't working label Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant