【问题标题】:React-router and jsx-no-bindReact-router 和 jsx-no-bind
【发布时间】:2017-07-11 19:06:24
【问题描述】:

我正在使用react-router 进行导航。我有一些组件需要登录才能看到。

因此我有这个:(来自 react-router 中的以下文档)

<Route
  {...rest}
  render={props =>
    isLoggedIn()
      ? <Component {...props} />
      : <Redirect
          to={{
            pathname: '/login',
            state: {from: props.location},
          }}
        />}
/>

但是,这样做有一个问题,因为jsx-no-bind 不允许使用箭头功能。解决这个问题的正确方法是什么?还是应该简单地忽略这一点,因为它由于某种原因不会受到相同的性能影响?

【问题讨论】:

  • 这方面有什么新的吗?

标签: react-router react-jsx jsx


【解决方案1】:

关于渲染道具的官方 React 文档:

https://reactjs.org/docs/render-props.html

本文档使用如下示例:

<DataProvider render={data => ( <h1>Hello {data.target}</h1> )}/>

请注意此处提到的注意事项: https://reactjs.org/docs/render-props.html#caveats

这包括使用 PureComponents

【讨论】:

    猜你喜欢
    • 2018-01-10
    • 2023-03-12
    • 2019-07-25
    • 2019-11-10
    • 2019-10-05
    • 2018-03-18
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多