使用history.push进行呼叫转移

时间:2019-06-08 16:00:57

标签: reactjs react-router

我在这里写的是代码

NSScrollView

我使用import React, { FC, Fragment, useEffect } from "react"; import { createBrowserHistory } from "history"; const history = createBrowserHistory(); const HandlerErr: FC<{ error: string }> = ({ error }) => { useEffect(()=>{ const time = setTimeout(() => {history.push(`/`)}, 2000); return(()=> clearTimeout(time)); },[error]) return ( <Fragment> <div>{error}</div> <div>{"Contact site administrator"}</div> </Fragment> ); }; 组件进行重定向。但由于某些原因,它HandlerErr / history.push (不起作用。I took a video

2 个答案:

答案 0 :(得分:2)

import React, { FC, useEffect } from 'react';
import { RouteComponentProps, withRouter } from 'react-router-dom';

interface OwnProps {
  error: string;
}

type Props = OwnProps & RouteComponentProps<any>;

const HandlerErr: FC<Props> = ({ error, history }) => {
  useEffect(() => {
   const timeout = setTimeout(() => {
     history.push(`/`);
   }, 2000);

   return () => { 
     clearTimeout(timeout);
   };
  }, [error]);

  return (
    <>
      <div>{error}</div>
      <div>Contact site administrator</div>
    </>
  );
};

export default withRouter(HandlerErr);

答案 1 :(得分:1)

您需要使用react-router-dom

的历史记录

喜欢

import React, { Component } from 'react'
import { withRouter } from 'react-router-dom'

class Test extends Component {
  render () {
    const { history } = this.props

    return (
     <div>
       <Button onClick={() => history.push('./path')}
     </div>
    )
  }
}

export default withRouter(Test)