Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Enfrentando un error al implementar props.history.push(/) en el componente funcional
import React, { useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; import { Button } from 'react-bootstrap'; import NavBarManu from './NavBarManu' const Login = () => { const [name, setName] = useState(""); const [password, setPassword] = useState(""); function login() { fetch("http://localhost:3000/login?q=" + name).then((data) => { data.json().then((resp) => { console.warn("resp", resp) if (resp.length > 0) { localStorage.setItem('login', JSON.stringify(resp)) //Facing error console.warn(this.props.history.push('list')) } else { alert("Please check username and password") } }) }) } return ( <div> <br /><h2>Please Login !</h2><br /> <input type="text" placeholder="enter name" name="user" onChange={(event) => setName(event.target.value)} /> <br /> <br /> <input placeholder="enter password" type="password" name="password" onChange={(event) => setPassword(event.target.value)} /> <br /> <br /> <button onClick={() => { login() }} >Login</button> </div> ); }; export default Login;

Tengo un error en console.warn(this.props.history.push('list')). La sintaxis es para componente de clase. Es por eso que está mostrando error. Me enfrento a algunas dificultades para implementarlo en componentes funcionales. Después de que el usuario presione el botón de inicio de sesión, quiero que la página se dirija a la página de "lista". Por favor ayuda . Dar una solución a esto. Alguien solucione este error. Si hay alguna otra manera mejor de abordar esto, dígaselo.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está creando un componente funcional y utilizando la sintaxis del componente de clase. Tus accesorios están vacíos.

this palabra clave se usa en componentes de clase para hacer referencia al estado.

Debería ser así:

 console.warn(history.push('list'))

Tienes que importar:

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

Y en el cuerpo del componente de inicio de sesión:

 import { useHistory } from "react-router-dom"; const Login = () => { const [name, setName] = useState(""); const [password, setPassword] = useState(""); const history = useHistory() function login() { fetch("http://localhost:3000/login?q=" + name).then((data) => { data.json().then((resp) => { console.warn("resp", resp) if (resp.length > 0) { localStorage.setItem('login', JSON.stringify(resp)) //Facing error console.warn(history.push('list')) } else { alert("Please check username and password") } }) }) } return ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!