effector / reflect

Easily render React components with everything you love from ☄️ effector.

Home Page:https://reflect.effector.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A proposal to rework the operator `variant` or add a new, more flexible one.

MiiZZo opened this issue · comments

The variant operator has a strange limitation in the form of the need to cast the store to a string. It seems to me that its api needs to be reworked in order to make the operator more flexible.

Some example of using:

const $user = createStore({ isAdmin: false });

const Component = cases({
  source: $user,
  cases: [
    { view: UserComponent, filter: (user) => !user.isAdmin },
    { view: AdminComponent, filter: (user) => user.isAdmin }, 
  ],
});

const AdminComponent = () => <div>admin></div>
const UserComponent = () => <div>user</user>
const $projects = createStore([]);
const projectCreated = createEvent();

$projects.on(projectCreated, (projects) => [...projects, {}]);

const Component = cases({
  source: $projects,
  cases: [
    { view: CreateYourFirstProject, filter: (projects) => projects.length === 0 },
    { view: ProjectsList, filter: (projects) => projects.length > 0 },
  ],
});

const ProjectsList = () => <list />
const CreateYourFirstProject = () => <button />

I've left the pr with the implementation of this operator (cases) separate from the variant if that idea sounds good.
P.S. I'm not sure, maybe my idea is bad, I'll be grateful if you help me understand this. And please forgive me for my bad english.

@MiiZZo Hi!
Sorry it took so long to reply

The idea sounds really cool, i like this api 👍

But I don't really like the idea of making it a new operator, since it more like an extended version of current variant - it would be confusing to have a few operators for the same purpose 🤔

I think, it could very well be an extension of variant, what do you think?
This way current variant will be basically a shorthand of your, more flexible, version

As i see it based on your PR - it could be merged with variant easily 🤔