question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you work with side effects?

See original GitHub issue

for example I have three actions that fetch data via HTTP: fetch, fetchSuccess, fetchError

is something like the following “ok”?

const actions = store => ({
    fetch: state => {
        const stateUpdate = { ...state, loading: true };
        fetchFromApi().then(data => store.fetchSuccess(stateUpdate, data), err => store.fetchError(stateUpdate, err));
        return stateUpdate;
    },
    fetchSuccess: (state, payload) => ({ ...state, data: payload, loading: false }),
    fetchError: (state, payload) => ({ ...state, loading: false, error: payload }),
});

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
malbernazcommented, Nov 12, 2017

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.

2reactions
malbernazcommented, Nov 12, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Deal With Side Effects of Medicine - WebMD
WebMD has tips to keep medication side effects from putting a damper on your life.
Read more >
Finding and Learning about Side Effects (adverse reactions)
Learning about Side Effects -- From Minor to Life Threatening -- Unwanted or Unexpected Drug Reactions.
Read more >
How to Deal With Employees on Medication
Ask the employee about the medication and its side effects. Take notes. Ask about the drug's warnings and when you should call emergency...
Read more >
Side effects: Medication, types of effect, cancer treatment
Many treatments and medical procedures can cause side effects, which can be positive but more ... According to the website, this is within...
Read more >
What Are The Side Effects Of Working For Your Organization?
The first step is to recognize what the side effects are of working for the organization. Then, take steps to address the side...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found