karterkortland
karterkortland

Reputation: 41

Is a forEach in redux reducer an anti-pattern?

I have a forEach loop which checks if a certain attribute of data coming in includes '/' if it does im replace the '/' with '-', i was wondering if that is valid redux code or im creating some sort of anti-pattern code right here.

case actions.GET_PRODUCTS: {
      const data = { ...payload };

      data.forEach(item => {
        item.options.forEach(option => {
          if (option.includes('/')) {
            option.cleanVersion = option.replace('/', '-');
          }
        });
      });

      return {
        ...state,
        items: data,
      };
    }

Or could i use something along the lines of normalizr to handle this? and if so an example would be appreciated

Upvotes: 0

Views: 254

Answers (1)

phry
phry

Reputation: 44146

A forEach in a reducer is perfectly fine.

You should be aware though that you are writing a style of Redux in genral that is outdated by years. Modern Redux does not use switch..case reducers, ACTION_TYPEs, immutable reducer logic, createStore or connect.

As a result, it's 1/4 of the code and much less prone to errors.

You can read up on modern Redux by following the official Redux tutorial.

Upvotes: 1

Related Questions