DEV Community

Discussion on: How to import a function from a functional component to another functional component

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..