Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the difference with a simpler useDialogState hook #40

Open
royriojas opened this issue Nov 7, 2020 · 3 comments
Open

What's the difference with a simpler useDialogState hook #40

royriojas opened this issue Nov 7, 2020 · 3 comments

Comments

@royriojas
Copy link

royriojas commented Nov 7, 2020

Hi @renchap

I'm really curious to understand why the useModal hook needs to use react context.

Basically the way I was handling modals was using this simpler hook.

export const useDialogState = (open = false) => {
  const [isOpen, setOpen] = useState(open);

  return [
    isOpen,
    useCallback(() => setOpen(true), []),
    useCallback(() => setOpen(false), [])
  ];
};

I've forked your demo and replaced with this simpler hook and the app seems to be working fine

https://codesandbox.io/s/usedialogstate-89k27?file=/src/useDialogState.js:48-258

So I was really curious why it was needed to use the context. Are there any advantages that I'm missing? or disadvantages on the simpler approach that I fail to see?

I'm kind of new to react hooks (I prefer to use Mobx for state management) and we used to have a small model to manage dialog states so it was kind of natural to me to just re-write it as a simpler hook but then I found your model and was surprised about the usage of react context.

@picosam
Copy link

picosam commented Jan 26, 2021

@mpontus I'm very interested in your answer to this as well! Thank you.

@andresmoschini
Copy link

+1

@azzztec
Copy link

azzztec commented Mar 19, 2024

When component with

Hi @renchap

I'm really curious to understand why the useModal hook needs to use react context.

Basically the way I was handling modals was using this simpler hook.

export const useDialogState = (open = false) => {
  const [isOpen, setOpen] = useState(open);

  return [
    isOpen,
    useCallback(() => setOpen(true), []),
    useCallback(() => setOpen(false), [])
  ];
};

I've forked your demo and replaced with this simpler hook and the app seems to be working fine

https://codesandbox.io/s/usedialogstate-89k27?file=/src/useDialogState.js:48-258

So I was really curious why it was needed to use the context. Are there any advantages that I'm missing? or disadvantages on the simpler approach that I fail to see?

I'm kind of new to react hooks (I prefer to use Mobx for state management) and we used to have a small model to manage dialog states so it was kind of natural to me to just re-write it as a simpler hook but then I found your model and was surprised about the usage of react context.

When your parent component is unmounted all your models will be unmounted too, but with context thay still be alive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants