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

Clarification: canDeactivate & forceDeactivate & autoCleanUp #451

Open
mahnunchik opened this issue Feb 1, 2020 · 1 comment
Open

Clarification: canDeactivate & forceDeactivate & autoCleanUp #451

mahnunchik opened this issue Feb 1, 2020 · 1 comment

Comments

@mahnunchik
Copy link

Hi, I've faced with not working canDeactivate by default. I've fixed it by the following code sample:

const router = createRouter([
  { name: 'messages', path: '/messages' },
  { name: 'messages.create', path: '/create' },
], {
  autoCleanUp: false,
});

router.usePlugin(browserPlugin({
  useHash: true,
  forceDeactivate: false,
}));

const canDeactivate = (router) => {
  return (toState, fromState) => {
    // Some conditions
    return false;
  }
}

router.canDeactivate('messages.create', canDeactivate);

router.start();

There are some related issue:

  1. Question: Combine Prevent Navigation + Async Data #175
  2. canDeactivate for nested routes #163
  3. canDeactivate not working properly #368

Question: what are the use cases of canDeactivate without forceDeactivate: false and autoCleanUp: false options?

@mahnunchik
Copy link
Author

Another related question: is it correct way to connect View with canDeactivate handler?

// autoCleanUp: true

// route 'messages.create'
const BlockedView = () => {
  const { router } = useRouteNode('messages.create');

  const [blocked, setBlocked] = useState(false);

  router.canDeactivate('messages.create', (router) => (toState, fromState) => {
    return !blocked;
  });
  return (
    <View>
      <Link onClick={() => setState(!blocked)}>Toogle {blocked ? 'true' : 'false'}</Link>
    </View>
  );
}

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

No branches or pull requests

1 participant