Skip to content

New function to programmatically navigate between routes #109

@bloom42-dev

Description

@bloom42-dev

Programmatical navigation with preact-iso is confusing due to the use of the useLocation hook which is limited by the rules of React hooks, rules that may not be intuitive.

From reading the issues, it seems that this is causing great confusion among users so I suggest a new API to change routes which would be more intuitive for users and would alleviate some of your support burden i.e.

#71
#54
#53
#97

I suggest a new function to navigate to a route that is not limited by the rules of React hooks.

import { pushRoute } from 'preact-iso';

function AnywhereInTheCode() {
    // Anywhere in the code, not only in places where hooks can be used
    pushRoute('/url');
}

Here are a few ideas of names for this function goTo, navigate, pushRoute.

From my reading of the code

function handleNav(state, action) {
it seems that the route function from useLocation does not fundamentally need to be a hook, so we could move it to its own dedicated function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions