arnelenero/simpler-state

Derived state

Closed this issue ยท 6 comments

Valtio and jotai both allow for having one store's value be derived from another store. Would be nice if simpleR state could do the same :)

Thanks for your submission.

I edited my previous response because I misunderstood what you meant by "store". So I assume you meant the units of state, whatever that is called in those 2 libraries that you mentioned (proxy, atom).

SimpleR State uses a different paradigm, in that you don't derive entities from other entities, but instead you can create derived values of an entity the way you need it inside a component, using a "transform function" (what other libraries refer to as "selector").

Based on user surveys done prior to v1 release, majority felt that adding stuff like that will add more syntax to the library, and therefore would defeat the goal of utmost simplicity. The consensus was that if transform functions can achieve the same intention, it would be sufficient.

Can you give an example of something that derived entity could achieve that a transform function couldn't?

Hey, @arnelenero I'm also looking for something like this. In my case, I'm having a partial entity that is based on the id of an URL slug. In my case, I would like to update my entity so it returns the new item when the URL changes. In your previous post, you were talking about a transform function. Would you have an example or maybe an example matching my scenario?

Hey, @arnelenero I'm also looking for something like this. In my case, I'm having a partial entity that is based on the id of an URL slug. In my case, I would like to update my entity so it returns the new item when the URL changes. In your previous post, you were talking about a transform function. Would you have an example or maybe an example matching my scenario?

Thanks for your comment.

Based on the limited information about your use-case, it appears that you will just need two things:

  • a mechanism to listen to changes in the URL (this is not necessarily done by state manager)
  • in your URL change listener, that's where you would call an action associated to the entity, to update its value

If my understanding of your use-case is correct, it seems you require a completely different solution from "derived state".

Thanks, @arnelenero, in the end, I've ended up doing something like this in a custom hook:

export const useSomething = () => {    
    const params = useParams<IParams>();
    const something: ISomething | undefined = MyStore.use(state => {
        if (!state.things) return;
        for (const thing of state.things) {
            if (thing.id === params.id) return thing;
        }
        return;
    });

    return something;
};

An then used it like so:

const something = useSomething();

in your opinion is there another more valid solution to this?

@synapse Now I fully understand what you're trying to achieve, and yes I completely agree with your logic here.

I would write it a bit shorter, like below, but it's essentially the same solution as yours ๐Ÿ˜€:

export const useSomething = (): ISomething | undefined => {    
    const params = useParams<IParams>();

    return MyStore.use(state => state.things?.find(thing => thing.id === params.id));
}

Seems like a solution has been identified using existing constructs and paradigm of simpler-state. No code change is required on the library, so I am closing the issue now.

Thank you for your issue submission.