Unable to pass onClose function to child component
See original GitHub issueI am using a modal to display a login form but I’m finding it difficult to pass the onClose function to my form component. This is what my code looks like :
File Name: index.tsx
const IndexPage: React.FC = () => {
const { isOpen, onOpen, onClose } = useDisclosure();
return (
<Button onClick={onOpen}>Login</Button>
<Modal
isOpen={isOpen}
onClose={onClose}
closeOnOverlayClick={false}
closeOnEsc={false}
isCentered
>
<ModalOverlay />
<ModalContent borderRadius={4}>
<ModalHeader>
<Box textAlign="center">
<Heading>Sign In</Heading>
</Box>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<LoginForm onClose={onClose} />
</ModalBody>
</ModalContent>
</Modal>
</Layout>
);
};
File Name: LoginForm.tsx
const LoginForm: React.FC<any> = (onClose) => {
const [email, setEmail] = useState("");
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
console.log("Email is ", email);
console.log("Email has been sent to user");
onClose();
}
return (
<Box my={8} textAlign="left">
<form onSubmit={handleSubmit}>
<FormControl isRequired>
<FormLabel>Email address</FormLabel>
<Input
type="email"
placeholder="Enter your email address"
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setEmail(e.target.value)}
/>
</FormControl>
<Button type="submit" width="full" mt={4}>
Log In
</Button>
</form>
</Box>
);
};
export default LoginForm;
I get an error saying onClose() is not a function. How can I fix this issue ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
MUI Dialog - Can't pass onClose to onClick inside dialog actions
I'm trying to create a reusable Dialog component based on MUI dialog. Here is my code ...
Read more >Passing data from child to parent component in ReactJS
We passed the function as a prop to the Child component, so the Child is able to call the function and pass it...
Read more >A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >The Guide to Learning React Hooks (Examples & Tutorials)
Learn all about React Hooks with this hands-on guide. Includes tutorials and code examples on using hooks for state and effects, for context...
Read more >Pass data or events from a parent component to a child ...
Pass data or event from a child component to parent component in both functional and class components.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That fixed it. I am new to TypeScript which cause this issue.
Thanks for the prompt response @with-heart 😃
@VarunRustomji The issue was just a small oversight in Segun’s example code.
Two possible solutions:
onClose
existence before calling: https://codesandbox.io/s/eloquent-leavitt-w5bsm?file=/src/App.tsx:1267-1293onClose
prop required: https://codesandbox.io/s/determined-wilson-4umzg?file=/src/App.tsx:998-1021https://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties has more information about optional properties.