I'm struggeling already some days now. I'm searching for a way to define a React component without UI. My goal is to have several functions in one "component" (as I said without UI), which can be used by other components, but with all the benefits of useState
etc.
I already, thought, there is a way, by defining components like this:
export const useExample = () => {
const [someVariable, setSomeVariable] = useState('someValue');
const getSomething = (parameter1, parameter2) => {
return ...
}
return {
getSomething
};
}
I thought, I could import useExample
in a component and then use the function getSomething
like this:
import { useExample } from 'useExample.component.js';
//...
function SomeComponent(props) {
//...
useEffect(() => {
const example = useExample.getSomething(parameter1, parameter2);
},[]);
//...
}
...but actually this seems not be working.
Then I get an error like this:
Uncaught TypeError:
...useExample.getSomething is not a function at eval...
All docs I find is telling me, that I need to return a JSX element. But I really cannot believe that in React there is no way to do this. Am I wrong? I know, if I use plain JS, I'm able to do so, but I already tried this approach and there are many other difficulties then with combining it with React components. Did anyone have had a similar issue and found a solution to this?