N
N
Nikolay Baranenko2021-11-15 10:45:07
React
Nikolay Baranenko, 2021-11-15 10:45:07

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);


app.js

<Route path='/profile/:userId'
           render={ () => <ProfileContainer /> } />


BUT in version 6
import error: 'withRouter' is not exported from 'react-router-dom'


how best to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2021-11-15
@Seasle

  • useParams
  • Migration from v5 to v6

N
Nikolay Baranenko, 2021-11-16
@drno-reg

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);

app.js
<Route path="/profile" element={<ProfileContainer />}>
        <Route path=":userId" element={<ProfileContainer />} />
      </Route>

it works 100% but not the best way and needs to be done differently but I'm solving a particular problem - I need to finish the job in the paradigm of IT React Kamasutra version 2019

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question