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

Maintaining State Between Calls #43

Open
k-funk opened this issue Dec 3, 2020 · 2 comments
Open

Maintaining State Between Calls #43

k-funk opened this issue Dec 3, 2020 · 2 comments

Comments

@k-funk
Copy link

k-funk commented Dec 3, 2020

First off, thanks for your work this project. Super lightweight and I'm sure is going to simplify my code a lot! 🙇 🙏


Question about default strategy for state inbetween calls

It can be annoying to have the previous async call result be "erased" everytime a new call is triggered (default strategy)

I'm curious why the default strategy was to not maintain previous result state? For nearly all my applications api calls, it looks something like

const [users, setUsers] = useState(undefined)
const [loading, setLoading] = useState(false)

async function getUsers() {
  setLoading(true)
  try {
    // to get behavior like react-async-hook, i'd add a `setUsers(undefined)` here
    const users = await GetUsers()
    setUsers(users)
  } catch (err) {
    setUsers(undefined)
  }
  setLoading(false)
}

What is gained by unsetting the result early? We already know when we are in a loading state so that we can choose to not render the old results if desired.

Improvement suggestions

Design questions aside, I'm wondering if

  1. A descriptively-named option ({ maintainState: true }, { resetResultOnLoading: false }, ...) could be added to do setLoading: state => ({ ...state, loading: true }), because really, I don't want to mess with any loading states, which this appears to do at first glance
  2. It would be great if options (like setLoading) could be more officially documented. I'm proposing using this library to my team, and I know when they see the setLoading option, they're going to be curious about what's happening there, then go to readme, and see it only within a code example.
@slorber
Copy link
Owner

slorber commented Dec 7, 2020

Hi,

You are right that maintaining the previous state should be the default behavior, and will likely change it and document it better for a version2 with some breaking changes like this.

@k-funk
Copy link
Author

k-funk commented Dec 7, 2020

🙏

If you have intentions of publishing a beta before a v2, let me know here in case there are other enhancements that you have planned and need someone to test out those changes in their working application..

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