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

React Context issue #1

Closed
MilosJo opened this issue Feb 14, 2019 · 6 comments
Closed

React Context issue #1

MilosJo opened this issue Feb 14, 2019 · 6 comments

Comments

@MilosJo
Copy link

MilosJo commented Feb 14, 2019

Hey there,
I really liked the idea of your API, but I have ran into an issue.
React version: "16.8.1"

This is the component where I use it.
cart is from my Context and toggleShowCart is a method on it, which works just fine.
"esc" gets logged to the console when I press it.

const Cart = ({ fixed, pinned, cart }) => {
  useHotKeys('esc', () => console.log('esc') || cart.toggleShowCart);
  return (
    <Wrapper
      fixed={fixed}
      pinned={pinned}
    >
      <Overlay isVisible={cart.isOpen} onClick={cart.toggleShowCart} />
      <Base
        fixed={fixed}
        pinned={pinned}
        isOpen={cart.isOpen}
      />
      . . .

Regards,
Milos

@JohannesKlauss
Copy link
Owner

JohannesKlauss commented Feb 14, 2019

Hi @MilosJo,
thanks for reporting the issue, but I am not quite sure what your problem is exactly. console.log('esc') || cart.toggleShowCart will always log esc to the console since it's a method call. Could you please specify what you expect the behavior should be and what it actually is? Thank you.

@MilosJo
Copy link
Author

MilosJo commented Feb 14, 2019

Hey @JohannesKlauss

Well, it is not invoking my function toggleShowCart properly.
As name states, it should open/close my cart component,
and its functioning properly when I use it like onClick={cart.toggleShowCart}.
It is not working tho, when I try to bind esc key to call this toggleShowCart method.

@JohannesKlauss
Copy link
Owner

@MilosJo That is because you are not invoking the function at all:

console.log('esc') || cart.toggleShowCart

You have to invoke the function inside the callback, since it is not the callback itself:

useHotKeys('esc', () => console.log('esc') || cart.toggleShowCart());

@MilosJo
Copy link
Author

MilosJo commented Feb 14, 2019

@JohannesKlauss
Sadly, I have tried that solution too, and it did not work :(

@JohannesKlauss
Copy link
Owner

Can you please provide a working codebox or pen example, where this issue is reproduced? Since the console.log('esc') is executed, the callback itself gets executed properly. Currently I don't see how this could be related to the hook, but a codebox example could deliver some answers.

@MilosJo
Copy link
Author

MilosJo commented Feb 15, 2019

Hey @JohannesKlauss
I tried it on creat-react-app, and copied most of the code and it worked like charm.
Anyways, I had deadline so I go with custom solution.
Thanks, your help is greatly appreciated.

@MilosJo MilosJo closed this as completed Feb 15, 2019
JohannesKlauss pushed a commit that referenced this issue May 29, 2019
Add callback to useEffect's second argument
JohannesKlauss pushed a commit that referenced this issue May 17, 2023
Update scoping-hotkeys.mdx, expect to except
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

2 participants