I'm using React-Router v4's Match object to pass down params to the next component. Where my Routing is like:
<Switch>
<Route exact path="/" component={ExperimentListPage} />
<Route path="/experiment/:experiment" component={ExperimentResultsPage} />
</Switch>
and my subcomponent looks like:
const ExperimentResultsPage = props => (
<ExperimentResultsContainer experimentName={props.match.params.experiment} />
);
export default withRouter(ExperimentResultsPage);
And it all works as intended, however ESLint is really unhappy with me using match.params.experiment
and it errors with [eslint] 'match.params.experiment' is missing in props validation (react/prop-types)
I saw in the React docs that I could use PropTypes.shape
however my params object but I was hoping there is a better way especially because the Match object contains a lot of fields.
Is there a better way of adding your Match route object to the props validation? How would that look like? If not, am I missing some other approach that could help solve this?
The exact prop is used to define if there is an exactly the requested path.
Another way to access the router props is to add an import statement at the top of the component and import 'withRouter'. import { withRouter } from 'react-router-dom'; Then in the export default statement at the end of the component you would wrap the component in 'withRouter'.
I ran into something similar, eslint seems fine with only declaring what Im using as props and ignoring unused fields from match:
match: PropTypes.shape({
params: PropTypes.shape({
experiment: PropTypes.string,
}),
}).isRequired,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With