Skip to content

[FEATURE] Support loops #9

@michaelboyles

Description

@michaelboyles

Currently, using a loop in a Redcr reducer can end up mutably editing the state. For example

interface StringState {
    str: String
}
const reducer = redcr((state: StringState) => {
    for (let i = 0; i < 3; i++) {
        state.str += 'A';
    }
});

Will produce

const reducer = (state) => {
    for (let i = 0; i < 3; i++) {
        state.str += 'A';
    }
    return state;
};

It should probably work similar to how if-statements are handled, and continuously reassign the state

const reducer = (state) => {
    for (let i = 0; i < 3; i++) {
        state = {
            ...state,
            str: state.str + 'A'
        }
    }
    return state;
};

Could explore loop unrolling too if the number of iterations is a compile-time constant.

How often is a loop actually useful in a reducer though?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions