Skip to content

Using this to set the state is no possible #544

Closed Answered by dai-shi
avifatal asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately, you seem to have some misconceptions with this behavior.

https://github.com/pmndrs/valtio#react-via-usesnapshot
Use of this is for expert users.
Valtio tries best to handle this behavior but it's hard to understand without familiarity.

Here's the fix, but I'd recommend to avoid using this.
https://codesandbox.io/s/valtio-to-do-list-forked-7k7bn0?file=/src/App.tsx


So, my recommendation is the following instead:

export const state = proxy({
  info: [{ status: "empty" }],
  loadAll: () => {
    axios.get(url).then((x) => {
      state.info = x.data;
    });
  },
});

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@avifatal
Comment options

@dai-shi
Comment options

@avifatal
Comment options

Answer selected by avifatal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants