Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withRouter HOC does not provide propTypes from wrapped component #4868

Closed
javaguru-ch opened this issue Mar 29, 2017 · 1 comment
Closed

Comments

@javaguru-ch
Copy link

The HOC withRouter should copy the prop-types and default-props definition from the wrapped component. This is currently not the case.
In order to implement this the following code may be used:

import React from 'react'
import Route from './Route'

/**
 * A public higher-order component to access the imperative API
 */
const withRouter = (Component) => {
  const C = (props) => (
    <Route render={routeComponentProps => (
      <Component {...props} {...routeComponentProps}/>
    )}/>
  )

  C.displayName = `withRouter(${Component.displayName || Component.name})`

  // Copy the prop-types as well as the default-props from the wrapped component:
  C.propTypes = Component.propTypes
  C.defaultProps = Component.defaultProps

  return C
}

export default withRouter
@timdorr
Copy link
Member

timdorr commented Mar 29, 2017

This isn't actually correct. withRouter's wrapper component isn't concerned with those props itself, so it shouldn't be validating them. Since we just pass though the props given to withRouter, they will be validated at the wrapped component level, as they should be.

If you need to access the wrapped component directly, #4838 should solve that for you when it lands.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants