DEV Community

Alexios Grigoriadis
Alexios Grigoriadis

Posted on

How to import a function from a functional component to another functional component

To make it clear... I have a submit button and when the user hits it i want to execute some functions that fetch some date and display some results. I can create those functions inside one component but it will be so big and i want to avoid that. So how can i write those functions on a separate js file and import them on the component that calls them ?

Top comments (2)

Collapse
 
ncpa0cpl profile image
ncpa0cpl

So how can i write those functions on a separate js file and import them on the component that calls them ?

The solution is just as you said here, wirte the function in a separate file and import it in the two component you want to use it and then use it. And you do it like any other function, just use import/export keyword.

Or you can go even one step further and make it a custom hook

Collapse
 
alexiosgrig profile image
Alexios Grigoriadis

Thanks fro the reply.. I think useContext will be a solution here. In this case, i want the functions to fetch some api and call them on a onClick att inside a input..