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

334
Views
Cómo llamar a la función desde el archivo App.js dentro de la carpeta pública en React JS

Me gustaría llamar a una función que se define dentro de src/App.js (callMe) desde la carpeta pública,

En App.js

 import messaging from './firebase-init'; import './App.css'; function App () { function callMe() { console.log('Call me function called'); } return ( <div className="App"> <h1>Hello World</h1> </div> ); } export default App;

Y tengo otro archivo firebase-messaging-sw.js ubicado dentro de la carpeta pública,

En firebase-messaging-sw.js

 importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js'); importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-messaging.js'); self.addEventListener('notificationclick', function (event) { console.log('On notification click: ', event); event.notification.close(); event.waitUntil(clients.matchAll({ includeUncontrolled: true, type: "window", }).then(function (clientList) { console.log('********* clientList: ', clientList); for (var i = 0; i < clientList.length; i++) { var client = clientList[i]; if (client.url !== '' && 'focus' in client) { client.focus(); callMe(); // This function is defined in src/App.js break; } } })); }); const firebaseConfig = { apiKey: "API_KEY", authDomain: "AUTH_DOMAIN", projectId: "PROJECT_ID", storageBucket: "STORAGE_BUCKET", messagingSenderId: "SENDER_ID", appId: "APP_ID", measurementId: "MEASUREMENT_ID" }; // Initialize Firebase const initializedFirebaseApp = firebase.initializeApp(firebaseConfig); const messaging = firebase.messaging();

¿Puede ayudarme, por favor, cómo debo llamar a la función callMe() ?

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

0

Puede intentar usar postMessage window.postMessage spec

simplemente envíe un mensaje en su archivo .js

 window.postMessage('someFunctionId','*')

y luego, en reaccionar, puede agregar eventListener para este mensaje (la mejor solución es useEffect y no olvide eliminarListener en la devolución de llamada)

 useEffect(()=>{ window.addEventListener("message",handlerFunc); return ()=> window.removeEventListener("message",handlerFunc); }

y encuentre su handlerFunc , puede verificar si el mensaje es el mismo que su ID que publicó ...

incluso puede pasar un objeto en lugar de una cadena para pasar algunos parámetros para su función

 window.postMessage({id:'someFunctionId',data:'test'},'*');
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!