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

124
Views
Página de redireccionamiento automático use websocket y reactJS

ahora estoy creando una combinación de proyectos con ReactJs, Websocket y nodeJS. Todavía soy nuevo en reactJs y necesito implementar la redirección automática después de obtener datos, use el websocket que se envía en nodeJs a reactJs. pero me cuesta cómo implementarlo.

aquí está mi código

 import React from 'react'; import { Redirect, Router, useHistory, useNavigate } from "react-router-dom"; import { io } from "socket.io-client"; const socket = io("ws://localhost:3000"); function Product(props) { socket.on("payment_123", (arg) => { // 1st try code // this.props.history.push('/address/payment_123'); // 2nd try code // this.setState({ redirect: '/address/payment_123' }); //3rd try code // browserHistory.push('/address/payment_123'); //4th try code // Router.transitionTo('/address/payment_123'); //5th try code // const navigate = useNavigate(); // navigate('/address/payment_123', { replace: true }); }); return ( <div> <div className="row"> <div className="col"> Product View Body </div> </div> </div> ); } export default Product;

Ya probé esos 5 códigos, pero nadie puede trabajar ... por favor, ayúdenme a cómo puedo redirigirlo automáticamente.

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

0

Puede hacer su lógica dentro de useEffect con la función async . Puede usar useHistory o useNavigate

 import React from 'react'; import { Redirect, Router, useHistory, useNavigate } from "react-router-dom"; import { io } from "socket.io-client"; function Product(props) { const history = useHistory(); //or you can use navigate // const navigate = useNavigate(); const socketTrigger = async () => { const socket = await io("ws://localhost:3000"); const socketOn = await socket.on("payment_123", (arg) => { //Here do your logic }); history.push('HERE PUT YOUR URL') //or use navigate here up to you } useEffect(() => { socketTrigger(); },[]) return ( <div> <div className="row"> <div className="col"> Product View Body </div> </div> </div> ); } export default Product;
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!