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

Operator proposal: toggler #286

Open
Shiyan7 opened this issue Jun 27, 2023 · 3 comments
Open

Operator proposal: toggler #286

Shiyan7 opened this issue Jun 27, 2023 · 3 comments
Labels
RFC Some new feature that should be discussed

Comments

@Shiyan7
Copy link

Shiyan7 commented Jun 27, 2023

A useful toggler helper that returns 3 events and a store

export const toggler = (defaultValue = false): TogglerInstance => {
  const open = createEvent();
  const close = createEvent();
  const toggle = createEvent();

  const $isOpen = createStore(defaultValue)
    .on(open, () => true)
    .on(close, () => false)
    .on(toggle, (v) => !v);

  return {
    open,
    close,
    toggle,
    $isOpen,
  };
};
export const toggler = toggler();

sample({
  clock: smthEvent,
  target: toggler.open
}}

In components it would look like this:

import { useUnit } from 'effector-react';

export function useToggler(togglerInstance: TogglerInstance) {
  const { $isOpen, open, close, toggle } = togglerInstance;

  return useUnit({ isOpen: $isOpen, open, close, toggle });
}
const { isOpen, open, close, toggle } = useToggler(someModel.toggler);

<button onClick={open}>Button</button>
@sergeysova sergeysova added the RFC Some new feature that should be discussed label Jun 28, 2023
@sergeysova
Copy link
Member

Looks like @@unitShape can be used instead of useToggler

@sergeysova
Copy link
Member

I propose a better name in the effector system: createToggle.

@Shiyan7
Copy link
Author

Shiyan7 commented Jun 30, 2023

I like)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFC Some new feature that should be discussed
Projects
None yet
Development

No branches or pull requests

2 participants