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

Side effects / async actions #1

Closed
diegohaz opened this issue Mar 31, 2018 · 9 comments
Closed

Side effects / async actions #1

diegohaz opened this issue Mar 31, 2018 · 9 comments
Labels
enhancement New feature or request

Comments

@diegohaz
Copy link
Owner

diegohaz commented Mar 31, 2018

I'm thinking on a way to handle side effects and/or async actions on constate. This is how I'm currently planning to use it:

const effects = {
  createPost: payload => async ({ state, setState, props }) => {
    try {
      const data = await api.post('/posts', payload)
      setState({ data })
    } catch (error) {
      setState({ error })
    }
  }
}

Possible names:

  • asyncActions
  • effects
  • thunks
  • methods
  • (suggestions are welcome)
@hiddenboox
Copy link

+1 for name it effects

@diegohaz
Copy link
Owner Author

diegohaz commented Apr 4, 2018

@hiddenboox My only problem with effects is that it may give the impression that it's not imperative. That is, people may think that it reacts to other actions and can't be called directly, similar to redux-saga, which isn't the case.

asyncActions is kind of ugly, but goes straight to the point. However, the function may not be async at all. People may want to use it only to have access to setState for some reason.

I'm more inclined to use thunks. Besides being familiar for who were used to redux-thunk, that makes total sense: https://en.wikipedia.org/wiki/Thunk

@joshuaquek
Copy link

You guys might wanna check out Rematch (2.8k stars)

https://github.com/rematch/rematch/blob/master/docs/purpose.md

For async methods, just do your regular async/await on it : https://github.com/rematch/rematch#step-2-models

@diegohaz
Copy link
Owner Author

diegohaz commented Apr 5, 2018

Thank you, @joshuaquek. I'd already taken a look at Rematch before. Never used it though. It looks like a good solution for people who are using Redux.

Constate has a different purpose. It's not intended to be a replacement for Redux. The problem is that people are using Redux for everything, even when they might not need it, making their projects unnecessarily complex upfront. And I had done that myself.

I built Constate to fill that void in cases when you don't need Redux, MobX (or Rematch). It's not even supposed to be the only solution for state management within a single project. People can combine it with other solutions. In the end, it's just a component (State).

I know you're bringing it up just as a reference, not for comparison, but I wanted to share my thoughts 😄

That said, it's good to know that they use effects as imperative routines. Although I still think it can be confusing.

@diegohaz
Copy link
Owner Author

diegohaz commented Apr 5, 2018

Another possible API is to pass actions instead of setState:

const actions = {
  setData: data => () => ({ data }),
  setError: error => () => ({ error })
};

const effects = {
  createPost: payload => async ({ setData, setError }) => {
    try {
      const data = await api.post('/posts', payload);
      setData(data);
    } catch (error) {
      setError(error);
    }
  }
};

Its more verbose though.

@zeloru
Copy link

zeloru commented Apr 7, 2018

i suggest Tasks like in .Net/C#: https://docs.microsoft.com/en-us/dotnet/csharp/async
or future like in C++ (std::future) or Java (java.util.concurrent.Future)

@guilherme6191
Copy link

asyncActions sounds good to me.

@jonjia
Copy link

jonjia commented Apr 11, 2018

what about asyncConstate

@diegohaz diegohaz added the enhancement New feature or request label Apr 14, 2018
@diegohaz diegohaz mentioned this issue Apr 21, 2018
@diegohaz
Copy link
Owner Author

Opened a PR (#8) adding effects (turns out that most of the libraries use this name). Would be nice to have some feedback from you guys. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants