Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot mutate a Store directly #4

Closed
kapilpipaliya opened this issue Jul 24, 2022 · 5 comments
Closed

Cannot mutate a Store directly #4

kapilpipaliya opened this issue Jul 24, 2022 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@kapilpipaliya
Copy link

kapilpipaliya commented Jul 24, 2022

Cannot mutate a Store directly

       const owner = getOwner();
       const attrs= createFormArray([]);
       ...
        runWithOwner(owner, () => {
            attrs.controls.splice(toIndex, 0, ...attrs.controls.splice(fromIndex, 1));
            for (let i = 0; i < attrs.controls.length; i++) {
              attrs.attrsontrols[i].controls.pos.setValue(i + 1);
            }
        });
@jorroll
Copy link
Owner

jorroll commented Jul 24, 2022

Correct, you need to use the built in methods. I'm very sorry that I don't appear to have mentioned this in the docs, but solid-forms is immutable unless using the built-in methods. Not mentioning that is a big oversight on my part. I'll correct that and update the docs to make clear that the API is immutable unless using the built-in methods.

You can see the methods on FormArray here: https://github.com/jorroll/solid-forms#iformarray

@jorroll jorroll added the documentation Improvements or additions to documentation label Jul 24, 2022
@kapilpipaliya
Copy link
Author

Thanks for the quick response. can you give me a way to modify array element positions, if possible?

@jorroll
Copy link
Owner

jorroll commented Jul 25, 2022

Hey @kapilpipaliya, I expect this will work for you:

// first we clone the control's `controls` value
const controls = [ ...attrs.controls ] // or attrs.controls.slice()
// then we mutate the cloned `controls` value
controls.splice(toIndex, 0, ...controls.splice(fromIndex, 1));
// then we use the `setControls()` method to update the control's `controls` value
attrs.setControls(controls);
// etc...

@jorroll
Copy link
Owner

jorroll commented Jul 25, 2022

I don't think it matters for your example, but, as mentioned in the Getting Started section, solid-forms just uses the Solidjs Store object under the hood. Solidjs stores wrap nested objects in proxies. If this is ever a problem or annoying (e.g. using console.log to inspect values in the browser) you can use the Solidjs unwrap function to access the original, non-proxied, control object.

@jorroll
Copy link
Owner

jorroll commented Jul 25, 2022

I updated the readme in multiple places to note that solid-forms control objects are immutable unless you use the provided methods to update their state.

@jorroll jorroll closed this as completed Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants