react-router:类型中缺少'history'

cc沈

App.tsx的一些代码:

interface AppProps{
  history: any
}

export default class App extends React.Component<AppProps,...> {

  public state = {
    target: this.props.history.push('/')
  };

  private route() {
    if (!session.isLogin) {
        this.setState({ target: this.props.history.push('/') });
        return
    } else {
        this.setState({ target: this.props.history.push('/root') })
    }
  }
  ...
  public render() {
    return this.state.target
  }
}

一些index.tsx的代码

ReactDOM.render(
  <MemoryRouter>
    <LocaleProvider locale={zhCn}>
        <App history={ ???? }/>
    </LocaleProvider>
  </MemoryRouter>,
  document.getElementById('root') as HTMLElement
);
registerServiceWorker();

为什么“历史”类型错误?提示:“ {}”类型中缺少属性“历史记录”。我应该在index.tsx中传递什么参数?

谢谢!

马特·麦考森

如果您想App接收来自的所有RouteComponentProps(包括历史记录对象)MemoryRouter,则需要App通过一条路线进行呼叫,如下所示:

ReactDOM.render(
  <MemoryRouter>
    <LocaleProvider locale={zhCn}>
        <Route component={App}/>
    </LocaleProvider>
  </MemoryRouter>,
  document.getElementById('root') as HTMLElement
);

然后,你可以替换AppProps使用RouteComponentProps<{}>中的声明App

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React router history.push 回退到 404 路由

使用react-router-dom的history.push

react-router重定向vs history.push

如何配置 react-native-router-flux android back history?

react-router,属性“ history”未定义

如何使用React的Router v4 history.push()

在React Router中,如何删除/更改我之前发送的history.push中的参数?

在 react-router-dom v6 中的 history.replace

React-router history.listen没有在componentWillMount中运行

如果react-router4中的位置不变,如何防止history.push?

如何访问React组件中的history.listen?

如何使用 history.push 在 React 中获取参数

如何在 React 中描述 history.goBack 的 TS 接口?

React-类型{}中缺少属性

react-router-redux和redux-immutable:您不能更改<Router history>;它将被忽略

React Router服务器端渲染错误:警告:失败的propType:在RoutingContext中未指定必需的prop`history`

模拟use-History挂钩的react-router-dom会导致以下错误-TS2698:传播类型只能从对象类型创建

如何解决此警告:“ React Hook useEffect缺少依赖项:'history'”?

window.history与Router.history返回

React Router 中 url 参数的 TypeScript 类型

Redux和React Router:梳理调度和导航(history.push)

反应Auth状态和Routes。无法在React-Router的'History'上执行'replaceState'

React-router-dom如何在不使用History的情况下使用goBack函数

React Router v5.2-使用createBrowserHistory和history.block阻止路由更改

无法读取未定义的属性“ history”(React Router 5的useHistory挂钩)

如何使用Jest通过调用history.push来测试React-Router导航?

使用react-router v4的this.props.history.push打开窗口?

history.location.replace不是使用react-router v4的功能

不确定如何使用react-router v4实现history.push