I have a button component which when clicked should render a modal to the screen, but for some reason it seems not to work, here’s my implementation
import React, { useCallback } from 'react';
import { OverflowMenuItem } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { AddItemToListModal } from '@myLib/commons-lib';
const AddToListOverflowMenuItem = ({ patientUuid }) => {
const { t } = useTranslation();
const handleClick = useCallback(() => AddItemToListModal, []);
return (
<OverflowMenuItem
itemText={t('addToList')}
onClick={handleClick}
style={{
maxWidth: '100vw',
}}
/>
);
};
export default AddToListOverflowMenuItem;
2
Answers
The problem is how you declare and use your function inside the useCallback. You’re doing it wrong.
To make it work, simply change your useCallback to be like this:
The useCallback first argument explanation:
For more information:
https://react.dev/reference/react/useCallback
With modal I’d normally work with some
isVisible
state that is set locally like that.. a modal can/should already be nested in the tree. This is just a basic example to show.. you’ll need to device how to hide and handle this prop inside your modal component