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.

If createMutator passed the mutate() method instead of the draft it could better support async

const { createMutator } = createState({
  value: "",
  loading: false,
  error: null,
});

const updateValue = createMutator(async (mutate, value) => {
  mutate(draft => { draft.loading = true; });
  try {
    await fetch('/api/v1/value', { method: 'POST', ... });
    mutate(draft => { draft.value = value; });
  } catch (error) {
    mutate(draft => { draft.error = error; });
  } finally {
    mutate(draft => { draft.loading = false; });
  }
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jamiebuildscommented, May 3, 2018

I’ve been looking at ways to write states w/ their mutators in the same module, that might actually be the best way

states/counter.js

import createState from 'react-copy-write';

export type State = {
  count: number
};

export const initialState: State = {
  count: 0
};

// Export Provider, Consumer, wish it wasn't awkward to _not_ export `mutate` (update)
export const { Provider, Consumer, mutate } = createState(initialState);

// mutators:
export function increment() {
  mutate(state => { state.count + 1 });
}

export function decrement() {
  mutate(state => { state.count - 1 });
}

// selectors?
export function selectCount(state) {
  return state.count;
}

components/Counter.js

import { Consumer, increment, decrement, selectCount } from '../states/counter';

export default function Counter(props: {}) {
  return (
    <Consumer selector={selectCount}>
      {count => (
        <>
          <span>{count}</span>
          <button onClick={decrement}>-</button>
          <button onClick={increment}>+</button>
        </>
      )}
    </Consumer>
  );
}
0reactions
jamiebuildscommented, Jun 18, 2018

Do you want to document something like the above pattern?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutators and Orchestrators · GitBook
Orchestrators are the Swiss army knife of Satchel. They may do a variety of things: Perform async operations. Typically the orchestrator will dispatch...
Read more >
Request: mutator to remove async/await · Issue #933 - GitHub
In a transition to using async/await from callbacks or Promises, these very important keywords can be lost in merges.
Read more >
Adding Mutators - Replicache Docs
Mutators are how you change data in Replicache. Mutators are arbitrary functions that run once on the client immediately (aka “optimistically”), ...
Read more >
Writing mutation resolvers | Full-Stack Quickstart
We've written all of the resolvers we need for schema fields that apply to query operations. Now, let's write resolvers for our schema's...
Read more >
Message Mutators • NServiceBus • Particular Docs
Message Mutators allow mutation of messages in the pipeline. ... i.e. a pending Task or a CompletedTask , or be marked async ....
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