Giter Site home page Giter Site logo

Comments (10)

malbernaz avatar malbernaz commented on May 18, 2024 2

exactly the point! Rule of thumb is: if your action is async use the store methods, otherwise just use the state parameter from the action.

from redux-zero.

malbernaz avatar malbernaz commented on May 18, 2024 2

Although thinking about this example, maybe it would be nice to perform something like this:

const getActions = ({ setState }) => ({
  async getTodos() {
    try {
      setState({ loading: true });
      
      const payload = await client.get("/todos");
      
      return { payload, loading: false };
    } catch (error) {
      return { error, loading: false };      
    }
  }
});

And this kind of action, probably have a better testability. It could be done roughly like this:

describe("todo actions", () => {
  let actions, store, listener, unsubscribe;
  beforeEach(() => {
    store = createStore();
    actions = getActions(store);
    listener = jest.fn();
    unsubscribe = store.subscribe(listener);
  });

  it("should fetch todos", async () => {
    nock('http://someapi.com/')
      .get('/todos')
      .reply(200, { id: 1, title: "test stuff" });

    await actions.getTodos();

    const [LOADING_STATE, SUCCESS_STATE] = listener.mock.calls.map(([call]) => call);

    expect(LOADING_STATE.loading).toBe(true);
    expect(SUCCESS_STATE.payload).toEqual({ id: 1, title: "test stuff" });
    expect(SUCCESS_STATE.loading).toBe(false);
  });
});

@matheusml, your opinion please.

from redux-zero.

k1r0s avatar k1r0s commented on May 18, 2024

I try to keep actions as pure as posible (state in / state out) and manage the side actions in the component with something like this to keep component as clean as posible

import { config } from 'http-decorator';

config.base = 'http://jsonplaceholder.typicode.com';
...
class SomeClass {
  @http('put')
  public someMethod (url: string, params?: any, error?, result?): void {
    // the actual body method will be executed after request was performed
    if (error) throw error
    ... do something with the result
  }
}
...
someClassInstance.someMethod('users');

// the same as doing
// $ curl -X PUT http://jsonplaceholder.typicode.com/users

Hope this may help.

from redux-zero.

matheusml avatar matheusml commented on May 18, 2024

Hi @seveves

That's a good question. Right now we don't have the full documentation, but we have some examples on dealing with async functions.

But, I think that your example is 100% fine.

@malbernaz have something to add here?

from redux-zero.

seveves avatar seveves commented on May 18, 2024

@k1r0s and @matheusml ... thank you for your answers!
I want to remove all the async http logic from my components to keep them as simple as possible but of course the actions should stay as pure as possible. As always you have to compromise. And redux-zero should stay small and simple so it's ok that the users have to find a way to go with.
But yeah ... maybe it would be helpful to have something about side effects in the documentation 👍

When @malbernaz has nothing to add here then feel free to close this "issue".

from redux-zero.

malbernaz avatar malbernaz commented on May 18, 2024

@seveves you are thinking as if you were using redux... In redux-zero we expose the store in the actions function to better support async. You don't need to have three actions to handle a fetch request, you can do something like this:

const actions = ({ getState, setState }) => ({
  fetch: state => {
    fetchFromApi()
      .then(payload => {
        setState({ payload, loading: false });
      })
      .catch(error => {
        setState({ error, loading: false });
      })

    return { loading: true };
  }
});

from redux-zero.

seveves avatar seveves commented on May 18, 2024

so const actions = store => ({ ... }) is the same as const actions = ({ getState, setState }) => ({ ... })? Not needing the three actions sounds very nice :)

from redux-zero.

matheusml avatar matheusml commented on May 18, 2024

If you have more questions @seveves feel free to ask.
For now, I'm closing this 👍

from redux-zero.

matheusml avatar matheusml commented on May 18, 2024

Since async/await is what the cool kids are doing, and the readability for this case is better, I think we should do it 👍

from redux-zero.

esperancaJS avatar esperancaJS commented on May 18, 2024

none of the examples above seem to be working :s

Only Loading get's updated, but the other state attributes don't

from redux-zero.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.