1

I have an array called products and inside of that I have a field called productCategory. My problem is that its clearing up other values of the array.

Codesandboxis here CLICK HERE

case appConstants.CHANGE_PRODUCT_CATEGORY:
  return {
    ...state,
    products: state.products.map((product) => {
      product?.productCode === action.payload?.productCode
        ? {
            ...product,
            productCategory: action.payload.productCategory
          }
        : product;
    })
  };
1
  • FYI, this will be much simpler if you use our official Redux Toolkit package to write your Redux logic, as it uses Immer to let you write "mutating" immutable state update logic without having to use a bunch of nested spread operators. Commented Dec 6, 2021 at 16:47

1 Answer 1

1

Maybe it's because you have missed return keyword inside map function.

case appConstants.CHANGE_PRODUCT_CATEGORY:
    return {
    ...state,
    products: state.products.map((product) => {
      return product?.productCode === action.payload?.productCode
        ? {
            ...product,
            productCategory: action.payload.productCategory
          }
        : product;
    })
};
Sign up to request clarification or add additional context in comments.

8 Comments

If my answer is correct, I would appreciate it if you mark it as accpeted response.
Also, when I type something in productName, it resets its value after selecting from ProductCategory?
Right, I didn't check your sandbox, I just guess the problem by your code, let's check your sandbox
Could you please take look at this SandBox to check it works crrectly or not?
Can you point what did you change? It seems that's my codesanbox link. It should a new link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.