Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
How can i export a function without passing it as props or using the export keyword?

I have a component like this

const ListOfMeds = () => {
  const mockListOfMedicines = [
    {
      medicineName: "Augmentin 625 Duo Tablet",
      medicineId: "D06ID232435454",
      groupName: "Generic Medicine",
      stock: 350,
    },
    {
      medicineName: "Augmentin 625 Duo Tablet",
      medicineId: "D06ID232435454",
      groupName: "Generic Medicine",
      stock: 350,
    },
  ];

  const getSpecificMedicineWithId = (number) => {
    const filteredData = mockListOfMedicines.find((medicine) => {
      return medicine.medicineId === number;
    });

    return filteredData;
  };



 return{
           <Some stuff/> 
       }


};
export default ListOfMeds;

i now have another component like this

import React from "react";
import { useParams } from "react-router-dom";

const MedicineInfo = () => {
  let params = useParams();

  return <div>MedicineId: {params.medicineId}</div>;
};

export default MedicineInfo;

I want the second component to call the function getSpecificMedicineWithId in the first component passing in the params objects key as the argument. However, the second component is not a child to the first component so I cannot pass that function as a prop. I also cannot use context because of also the same reason. It's not its child.

If I use export on the first component I get the errors

Modifiers cannot appear here.

and

'import' and 'export' may only appear at the top level. (58:2)

I can't take the getSpecificMedicineWithId function out of its component so as to export it because it depends on the data inside the component to work i.e the mockListOfMedicines array.

Now I'm stuck I don't know how to go about it. Kindly Help.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

To achieve this, you must declare and export the function outside the component. Because your data is fixed here, you can have the function anywhere you want. (Maybe you should put it in an helper file for more cleanliness.)

// ListOfMeds.jsx
export const getSpecificMedicineWithId = (number) => {
  const mockListOfMedicines = [
    {
      medicineName: "Augmentin 625 Duo Tablet",
      medicineId: "D06ID232435454",
      groupName: "Generic Medicine",
      stock: 350,
    },
    {
      medicineName: "Augmentin 625 Duo Tablet",
      medicineId: "D06ID232435454",
      groupName: "Generic Medicine",
      stock: 350,
    },
  ];

  const filteredData = mockListOfMedicines.find((medicine) => {
    return medicine.medicineId === number;
  });

  return filteredData;
};

const ListOfMeds = () => {
  return <Some stuff /> 
};

export default ListOfMeds;
// MedicineInfo.jsx
import React from "react";
import { useParams } from "react-router-dom";

import { getSpecificMedicineWithId } from "./ListOfMeds";

const MedicineInfo = () => {
  let params = useParams();

  const medicine = getSpecificMedicineWithId(parrams.medicineId);

  return <div>MedicineId: {params.medicineId}</div>;
};

export default MedicineInfo;
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda