Home > Net >  how solve this error (Failed to load resource: the server responded with a status of 404 ())
how solve this error (Failed to load resource: the server responded with a status of 404 ())

Time:01-17

i'm using "react with axios" to get data from fake API ( enter image description here

CodePudding user response:

You have to wrap the class component inside of withRouter since hooks won't work with class based components.

......................... UserPage.js .........................

import React, { Component } from 'react'
import {getUser} from '../../Api/Users-axios'
import ViewUserComp from '../ViewUserComp/ViewUserComp'
import {withRouter} from 'react-router'


export class UserPage extends Component {
 state = {
   user:{}
}
componentDidMount = () => {
    console.log(this.props)     
    const id = this.props.match.params.id; // try this one
    console.log('my id:' );
    console.log(id);
    getUser(id).then(response => {
        this.setState({
            user: response.data
        });
    })
        .catch(error => {
            alert('error');
        });
}

render() {
    return (
    <div>
        <h2>User Page</h2>
        <ViewUserComp user={this.state.user} />     
    </div>
    )
  }
 }
 export default withRouter(UserPage); // wrap it 

CodePudding user response:

This is a hook -> useParams() import it from 'react-router-dom'

  •  Tags:  
  • Related