Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

380
Visualizações
Cannot read properties of undefined (reading 'push')

Below is the code of ListEmployeeComponent

import React, { Component } from 'react';
import EmployeeService from '../service/EmployeeService';


class ListEmployeeComponent extends Component {
    constructor(props){
        super(props)

        this.state = {
            employees : []
        }

        this.addEmployee = this.addEmployee.bind(this);
    }

    componentDidMount(){
        EmployeeService.getEmployees().then((res) => {
           this.setState({ employees: res.data});
        });
    }

    addEmployee(){
        this.props.history.push("/add-employee");
    }
....
....
export default ListEmployeeComponent

And this one is of App.js code

import React from 'react';
import logo from './logo.svg';
import './App.css';
import {BrowserRouter as Router} from 'react-router-dom';
import {Routes, Route} from 'react-router';
import ListEmployeeComponent from './Component/ListEmployeeComponent';
import HeaderComponent from './Component/HeaderComponent';
import FooterComponent from './Component/FooterComponent';
import CreateEmployeeComponent from './Component/CreateEmployeeComponent';

function App() {
  return (
    <div>
        <Router>
         
                  <HeaderComponent/>
                     <div className="container">
                       <Routes> 
                       <Route exact path="/" element={<ListEmployeeComponent/>}/>
                       <Route path="/employees" element={<ListEmployeeComponent/>}/>
                       <Route path="/add-employee" element={<CreateEmployeeComponent/>}/>
                       </Routes>
                     </div> 
                     
                  <FooterComponent/>
           
        </Router>
    </div>
    
  );
}

export default App;

I want to navigate my page to the add-employee page but when I click on that button I'm getting an error as "Cannot read properties of undefined (reading 'push')"

Can anyone please help me with this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is because history is undefined when you call this.props.history.push();. You must pass that prop down to your ListEmployeeComponent component. You could also transform that component into a functional component like your <App /> component so that you can utilize a hook called useNavigate from react-router-dom. Below is a snippet that shows how we could utilize useNavigate so that we don't have to pass props down the component tree. Again, you will need to convert your ListEmployeeComponent component to a functional component for this to work: '

import { useNavigate } from 'react-router-dom';

export default function LinkButton({ children }: LinkButtonProps) {
    const navigate = useNavigate();

    function handleNavigate() {
        navigate('/dashboard');
    }

    return (
        <LinkButtonComponent onClick={handleNavigate}>
            {children}
        </LinkButtonComponent>
    );
}

about 4 years ago · Juan Pablo Isaza Relatório

0

import { useHistory } from "react-router-dom";

let history = useHistory();

In react there is useHistory hook which can be used like this instead of this.props.push. This will solve your problem.

about 4 years ago · Juan Pablo Isaza Relatório

0

Note that you get the history value from the props, but when defining the component in the program, you did not add any properties called history to the component.

function App() {
 ...
 return (
   ...
   <Route exact path="/" element={<ListEmployeeComponent histoy={}/>}/>
   ...
 )
 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda