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

Computed properties fire when an action is emitted #446

Closed
onionhammer opened this issue Mar 12, 2020 · 5 comments
Closed

Computed properties fire when an action is emitted #446

onionhammer opened this issue Mar 12, 2020 · 5 comments

Comments

@onionhammer
Copy link

I'm attempting to use computed properties to preserve fetch results and createContextStore;

...
getSomething1: computed((state) => fetch("something1", state.date)),
getSomething2: computed((state) => fetch("something2", state.date)),
getSomething3: computed((state) => fetch("something3", state.date)),
...

I'm then consuming only 1 of the computed properties; getSomething1, everything works perfectly until I change the state of 'date', then all three getSomething (1, 2, 3) fire off fetches even though I'm only using the first one anywhere.

@ctrlplusb
Copy link
Owner

Hey @onionhammer

That definitely should not be the case unless all the properties are currently being accessed by one of your components.

Would you mind creating a minimal reproduction for me on codesanbox? This will help me investigate it quicker for you.

👍

@onionhammer
Copy link
Author

@ctrlplusb Thanks for the response

I reproduced it in this repository: https://github.com/onionhammer/repro-easypeasy

@ctrlplusb ctrlplusb added this to the 4.0.0 milestone Mar 15, 2020
@ctrlplusb
Copy link
Owner

Thanks for the report @onionhammer

The investigation into this has led me to a really great insight. I'll definitely be improving this experience, which will resolve the issue you are experiencing.

As a side note though; I don't recommend putting your fetch requests within a computed property. Computed properties should be idempotent, and therefore should not contain side effects.

Also, a question regarding this. It seems that you are trying to encapsulate the fetch requests within Easy Peasy, but do you have any intention of storing the results of the request in your store state?

Based on the answer to my question I will recommend an alternative approach. 👍

@ctrlplusb ctrlplusb changed the title All computed properties fired on re-render Computed properties fire when an action is emitted Mar 15, 2020
@onionhammer
Copy link
Author

@ctrlplusb I've already switched to using react-query to cache results, but I'm definitely open to recommendations on how I can better use easy-peasy to do this stuff ;)

My initial goal was to store the Promise itself in the computed property, so if multiple things await it it will return a precomputed result if it's already been fetched

@ctrlplusb
Copy link
Owner

Using react-query sounds like a much better fit for what you were attempting, by way of the code you showed me. 👍

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

Successfully merging a pull request may close this issue.

2 participants