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

Proposal: Delayed Redirect #6422

Closed
GGAlanSmithee opened this issue Oct 25, 2018 · 3 comments
Closed

Proposal: Delayed Redirect #6422

GGAlanSmithee opened this issue Oct 25, 2018 · 3 comments

Comments

@GGAlanSmithee
Copy link

Delayed Redirect

Use Case

Display a page for n seconds, then redirect to another page.

Possible current solutions

class DelayedRedirect extends React.Component {
    state = {
        redirect: false,
    }

    componentDidMount() {
        setTimeout(() => {
            this.setState({
                redirect: true,
            })
        }, 2000)
    }

    render() {
        if (this.state.redirect) {
            return (
                <Redirect to={'/'} />
            )
        }

        return (
            <div>
                Redirecting to "/" in two seconds
            </div>
        )
    }
}

or

class DelayedRedirect extends React.Component {
    componentDidMount() {
        setTimeout(() => {
            history.push('/')
        }, 2000)
    }

    render() {
        return (
            <div>
                Redirecting to "/" in two seconds
            </div>
        )
    }
}

The above examples require you to use a proper (non-funcitonal) component and are either verbose or imperative (both which seems to go against the latest iterations of ´react-router´).

Propsed Solution / Interface

const DelayedRedirect = () => (
    <div>
        Redirecting to "/" in two seconds

        <Redirect to={'/'} delay={2000} />
    </div>
)

Disclosure

I have no idea of the inner workings of react-router and how feasible this would be or not.

@GGAlanSmithee
Copy link
Author

GGAlanSmithee commented Oct 25, 2018

Reference Implementation

import * as React from 'react'
import { Redirect, RedirectProps } from 'react-router'

interface DelayedProps {
    delay: number
}

interface DelayedState {
    timeToRedirect: boolean
}

class DelayedRedirect extends React.Component<
    RedirectProps & DelayedProps,
    DelayedState
> {
    timeout: any = null

    state: DelayedState = {
        timeToRedirect: false,
    }

    componentDidMount() {
        this.timeout = setTimeout(() => {
            this.setState({
                timeToRedirect: true,
            })
        }, this.props.delay)
    }

    componentWillUnmount() {
        clearTimeout(this.timeout)
    }

    render() {
        const { delay, ...props } = this.props
        const { timeToRedirect } = this.state

        if (timeToRedirect) {
            return <Redirect {...props} />
        }

        return null
    }
}

export default DelayedRedirect

Usage

import * as React from 'react'
import { DelayedRedirect } from 'react-router-dom'

const Test = () => (
    <div>
        Redirect to "/" in five seconds

        <DelayedRedirect to={'/'} delay={5000} />
    </div>
)

export default Test 

@GGAlanSmithee
Copy link
Author

Created a gist

@timdorr
Copy link
Member

timdorr commented Oct 25, 2018

Thanks, but I don't think this is going to be something we'll build into the library. There are a number of ways you might want to build it, so it's best left up to the user to determine how they want to implement it.

@timdorr timdorr closed this as completed Oct 25, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Dec 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants