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

How to apply splice with seamless-immutable? #251

Open
etairi opened this issue Jun 5, 2018 · 1 comment
Open

How to apply splice with seamless-immutable? #251

etairi opened this issue Jun 5, 2018 · 1 comment

Comments

@etairi
Copy link

etairi commented Jun 5, 2018

I'm using seamless-immutable inside React app, and I have a code segment like this in my saga:

let parsedData = yield select(state => state.post.posts);
parsedData = postUtils.deleteLike(parsedData, payload);

And my deleteLike method looks like this:

const deleteLike = (parsedPosts, likeId) => {
    let posts = Immutable.asMutable(parsedPosts);

    Object.entries(posts).forEach(
        ([_, userPosts]) => {
            Object.entries(userPosts).forEach(
                ([_, post]) => {
                    const found = post.likes.filter(l => l.id === likeId);
                    if (found.length > 0) {
                        const index = post.likes.indexOf(found[0]);
                        post.likes.splice(index, 1);
                    }
                }
            );
        }
    );

    return posts;
};

So, basically I want to remove one element from one of the inner arrays that is found inside the object. Though, the above code throws an error saying that splice cannot be invoked on an Immutable data structure. I though the call to asMutable() is going to convert it to a mutable object and store in posts variable. So basically asMutable() doesn't make the nested object mutable all the way? Because I also need at later stage to push into post.likes, not just remove an element as above. So, I want to make the whole object mutable so that I can work comfortably.

@crudh
Copy link
Collaborator

crudh commented Sep 2, 2018

@etairi Immutable.asMutable(obj, {deep: true}) should do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants