Given the following functional component, how do I export someFunction
without breaking it out into a separate file , so that I may test it?
const MyComponent = () => {
const someFunction = () => {
...
return someValue;
};
const [value, setValue] = useState(0);
useEffect(() => {
setValue(someFunction());
}, []);
return (
<div>
...
</div>
);
};
Adding an export in front of the function declaration does not seem to work.
Like @yury-tarabanko mentioned the export
keyword can only be used in the module scope, aka the file root.
export const someFunction = () => {
...
return someValue;
};
export const MyComponent = () => {
const [value, setValue] = useState(0);
useEffect(() => {
setValue(someFunction());
}, []);
return (
<div>
...
</div>
);
};
Just do export default function FunctionName(){ }
or
const MyComponent = () => {
const someFunction = () => {
...
return someValue;
};
const [value, setValue] = useState(0);
useEffect(() => {
setValue(someFunction());
}, []);
return (
<div>
...
</div>
);
};
export default Mycomponent
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.