I am new to react and javascript. I have been learning react and react router and a tutorial i was following was using withRouter. Anyway, I found some code how to reimplement withRouter as it is no longer supported. My question is the following. How does the internal function get the props argument. Where does it come from? Thank you all in advance for the response, I have been stuck on this for a while.
function withRouter(Component) {
function ComponentWithRouterProp(props) {
let location = useLocation();
let navigate = useNavigate();
let params = useParams();
return (
<Component
{...props}
router={{ location, navigate, params }}
/>
);
}
And this is the whole snippet of the Component code:
import { Routes, Route, Redirect} from 'react-router-dom'
import { connect } from 'react-redux';
import React, { Component } from 'react';
import {
useLocation,
useNavigate,
useParams
} from "react-router-dom";
function withRouter(Component) {
function ComponentWithRouterProp(props) {
let location = useLocation();
let navigate = useNavigate();
let params = useParams();
return (
<Component
{...props}
router={{ location, navigate, params }}
/>
);
}
return ComponentWithRouterProp;
}
const mapStateToProps = state => {
return {
dishes: state.dishes
}
}
const HomePage = () => {
return (
<div>home page component</div>
);
}
class MainPage extends Component {
constructor(props) {
super(props);
}
render() {
return(
<div>
<div>main component {this.props.dishes}</div>
<Routes>
<Route path='/home' element={<HomePage />} />
</Routes>
</div>
);
}
}
export default withRouter(connect(mapStateToProps)(MainPage));