React Hooks with React Router v4 - how do I redirect to another route?

后端 未结 3 1716
鱼传尺愫
鱼传尺愫 2021-02-13 05:30

I have a simple react hooks application - a list of Todos - with react router v4

On the List of Todos, when a Todo is clicked I need to:

  1. Dispatch the curre
3条回答
  •  情深已故
    2021-02-13 05:51

    It's actually a lot simpler than the other answers, React Router v5.1 provides a useHistory hook.

    import React from 'react'
    import { useHistory } from 'react-router-dom'
    
    const MyComponent = () => {
      const history = useHistory()
      const handleButtonClick = (event) => {
        history.push(event.target.value)
      }
      return (
        
      )
    }
    

提交回复
热议问题