Answer the question
In order to leave comments, you need to log in
How to correctly implement withRouter in the realities of [email protected] and [email protected]?
In a version less than react-router-dom and react-router there is less withRouter
and in order to read the value of userId you can do this
ProfileContainer.jsx
import {withRouter} from "react-router-dom"
class ProfileContainer extents React.Component{
componentDidMount(){
let userId = this.props.match.params.userId;
if (!userId){
userId=20771;
}
axios.get(`https://social-network.samuraijs.com/api/1.0/profile/`+userId)
.then(response => {
// debugger;
this.props.setUserProfile(response.data);
});
}
}
let WithUrlDataContainerComponent = withRouter(ProfileContainer);
<Route path='/profile/:userId'
render={ () => <ProfileContainer /> } />
import error: 'withRouter' is not exported from 'react-router-dom'
Answer the question
In order to leave comments, you need to log in
ProfileContainer.js
import React from "react";
import Profile from "./Profile";
import * as axios from "axios";
import {setUserProfile} from "../../redux/profile-reducer";
import {connect} from 'react-redux';
import {useParams} from "react-router-dom";
const withRouter = WrappedComponent => props => {
const params = useParams();
// etc... other react-router-dom v6 hooks
return (
<WrappedComponent
{...props}
params={params}
// etc...
/>
);
};
class ProfileContainer extends React.Component{
componentDidMount() {
// debugger;
//
console.log(this.props);
let userId = this.props.params.userId;
if (!userId){
userId=2;
}
axios.get(`https://social-network.samuraijs.com/api/1.0/profile/`+userId)
.then(response => {
// debugger;
this.props.setUserProfile(response.data);
});
}
render () {
// debugger;
return(
<Profile {...this.props} profile={this.props.profile}/>
)
}
}
let mapStateToProps = (state) => ({
profile: state.profilePage.profile
});
let WithUrlDataContainerComponent = withRouter(ProfileContainer);
export default connect(mapStateToProps, {
setUserProfile
}) (WithUrlDataContainerComponent);
<Route path="/profile" element={<ProfileContainer />}>
<Route path=":userId" element={<ProfileContainer />} />
</Route>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question