remix-run / history

Manage session history with JavaScript
MIT License
8.28k stars 961 forks source link

Manipulating history object from anywhere without warning #901

Open adammartiska opened 2 years ago

adammartiska commented 2 years ago

Hello, I would like to know if there is any possibility to use history.block() multiple times in the code :

  1. I am blocking re-routing from useEffect hook listening to some state changes and I am returning false in order to block redirect : React.useEffect(() => { return history.block((location) => { if (areTestPointParametersDirty) { service.send({ type: 'PAGE_ROUTE_INITIATED', url: location.pathname }) return false } return true }) }, [service, areTestPointParametersDirty])

  2. Now in my state machine (xstate lib) after some user action I want to redirect to that URL. I am unblocking history and pushing new url : redirectToIntendedUrl(context, event) { history.block(() => true) // either its event from saving parameters -> redirecting // or it is event from pressing proceed without save button history.push(event.data?.originalEvent?.url ?? event.url) }

Every time I am manipulating history.block from these 2 places in my code I am getting warning in my console: A history supports only one prompt at a time . Is it possible to get rid of this warning? I am thinking of passing block function also to my unblocking function. Or is there any other way to force push some URL, which won't check if the history is blocked? Thank you :)