preactjs / preact-router

:earth_americas: URL router for Preact.
http://npm.im/preact-router
MIT License
1.02k stars 155 forks source link

Alternative to componentWillMount in Redirect #361

Closed donkeyDau closed 4 years ago

donkeyDau commented 4 years ago

The README proposes this code to create a redirect.

import { Component } from 'preact';
import { route } from 'preact-router';

export default class Redirect extends Component {
  componentWillMount() {
    route(this.props.to, true);
  }

  render() {
    return null;
  }
}

but according to https://preactjs.com/guide/v10/components#lifecycle-methods componentWillMount is deprecated. Which lifecycle method is a proper replacement?

gtsop commented 4 years ago

Any updates on this please?

I ended up using the useEffect hook. Not exactly the same but I don't mind the slight delay

developit commented 4 years ago

componentWillMount is not deprecated in Preact. The Redirect implementation posted here seems fine to me.

donkeyDau commented 4 years ago

So why is it labeled as (deprecated) in the docs (https://preactjs.com/guide/v10/components#lifecycle-methods)?