【问题标题】:React typescript, what are the render props?React typescript,什么是渲染道具?
【发布时间】:2021-09-08 18:36:33
【问题描述】:

我正在尝试设置一个路由器,其丢失的路由将重定向到 /error ,问题是打字稿不允许我使用渲染

const App: React.FC<RouteComponentProps> = ({ history }) => {
  return (
    <Switch>
      <Route path="/" component={ChartContainer} exact />
      <Route path="/error" component={Error} />
      <Route path="/" render={() => history.push("")} />
    </Switch>
  )
}

index.d.ts(92, 5): 预期类型来自属性'render' 这是在类型“IntrinsicAttributes & IntrinsicClassAttributes> & Readonly & OmitNative>> & Readonly<...>'

我必须使用哪些类型?以及如何输入render?

【问题讨论】:

  • 我真的不明白这条路线&lt;Route path="/" render={() =&gt; history.push("")} /&gt;。你想做什么?
  • 重定向到/在无效路由器@GiovanniEsposito 上。因为我没有特定的 404 组件

标签: reactjs typescript


【解决方案1】:

使用Redirect 代替history

<Route path="/" render={() => <Redirect to="/your-path" />} />

【讨论】:

    【解决方案2】:

    render props 接受一个应该返回 JSX 组件(或 React 可以渲染的东西)的函数

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 1970-01-01
      • 2021-05-06
      • 2017-06-02
      • 2021-07-22
      • 2018-07-07
      • 2022-01-20
      • 2019-04-01
      • 1970-01-01
      相关资源
      最近更新 更多