question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to pass onClose function to child component

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
VarunRustomjicommented, Aug 21, 2020

That fixed it. I am new to TypeScript which cause this issue.

Thanks for the prompt response @with-heart 😃

0reactions
with-heartcommented, Aug 21, 2020

@VarunRustomji The issue was just a small oversight in Segun’s example code.

Two possible solutions:

https://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties has more information about optional properties.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found