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

[BUG] Using the result of a mutation as a local variable results in original state being modified #13

Open
michaelboyles opened this issue Nov 6, 2021 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@michaelboyles
Copy link
Owner

michaelboyles commented Nov 6, 2021

Describe the bug

When the result of a mutation, e.g. a unary operator, is assigned to a local variable, and that local variable is used in another mutation, e.g. assignment, the result can be that the original state is modified.

Sample input

An example in TypeScript of a reducer which produces unexpected results.

interface State {
    first: number;
    second: number;
}

const reducer = redcr((state: State) => {
    const foo = state.first++;
    state.second = foo;
});

Current output

Specify which ES version you're targeting: ES2020

const reducer = (state) => {
    state = {
        ...state,
        second: state.first++
    };
    return state;
};

In this case, state.first has been altered. The result should be something like

const reducer = (state) => {
    return {
        ...state,
        first: state.first + 1,
        second: state.first + 1
    };
};
@michaelboyles michaelboyles added the bug Something isn't working label Nov 6, 2021
@michaelboyles michaelboyles self-assigned this Nov 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant