preactjs / preact-router

:earth_americas: URL router for Preact.

Home Page:http://npm.im/preact-router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

route() does not routing anywhere

TheLastochka opened this issue · comments

commented

I want to redirect, for example, after a successful login, to the home page. And for this I wait for a response, and then redirect via route('/'), but nothing happens.
This is my login page

export const LoginPage = ({ handleLogin, fromPath }) => {
    const handleSubmit = async (event) => {
      event.preventDefault();
      const { username, password } = event.target;
      const response = await fetch(env.SERVER_URL + '/login', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ username: username.value, password: password.value }),
      });
      if (response.ok) {
        const { message, token } = await response.json();
        route(fromPath, true);
      } else {
        // Handle login error
        alert('Login failed!');
      }
    };
  
    return (
      <div>
        <h1>Login Page</h1>
        <form class="login-form" onSubmit={handleSubmit}>
          <input type="text" name="username" placeholder="Username" required />
          <input type="password" name="password" placeholder="Password" required />
          <button type="submit">Login</button>
        </form>
      </div>
    );
  };

But it does not route this not only on the login page. It doesn't work anywhere. What am I doing wrong?

And at the same time routing trough Router and Route components work fine. And Link component also redirects fine.

commented

So... do not mix packages)
My Route and other route components were from package preact-iso. I replaced the package with preact-router and everything started working fine.