Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using initial state for reseting the store#106

Ledespana started this conversation inGeneral
Discussion options

Hi!
I was wondering if this is a good practice, where you would do something like this to reset the store to an initial state:

const initialValuesStore = { name: 'placeholder'}
const resetForm = () => {    formStore.currentState = formStore.initialState; };

I was confused since that kind of works but not if you something like an array.
for example if you have something like this, it won't replace the content of the array:

const initialValuesStore = { forms = []}
 const resetForms = () => {     formStore.currentState = formStore.initialState;  };
You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Hi@Ledes ,

This wouldn't be good practice because the replacing of the store's state will happen outside of pullstate's internal mechanisms to keep your UI updated.

The better way to do this kind of thing would be to make use ofStore.replace(freshState).

Or, since I see you're doing a form- maybe your use-case is to have temporary state that can be quickly initialized when loading up the form for the first time. For this you should look atStore.useLocalCopyInitial() - this gives you the power of Pullstate but in a local component (this will not be a global store that you can control elsewhere though). I use this pattern in my apps- not everything has to exist in a global store, especially once-off form type things.

You must be logged in to vote
1 reply
@Ledespana
Comment options

Thanks for the replay@lostpebble!
replace definitely works. I didn't see it in the docs but it makes more sense of what I was doing.
About useLocalCopyInitial is also another option that I dind't see. That will make me doubt my current design.
Thanks!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@Ledespana@lostpebble

[8]ページ先頭

©2009-2025 Movatter.jp