Search code examples
reactjsreduxstore

TypeError: Cannot destructure property 'product' of 'productDetails' as it is undefined


**ProductDetailsScreen.js >>> Here is my code, I got the product list from Redux store, But facing a problem when initializing product details. **

export default function ProductDetilsScreen(props) {
  const dispatch = useDispatch();
  const productId = props.match.params.id;
  const productDetails = useSelector((state) => state.productDetails);
  const { loading, error, product } = productDetails;

  useEffect(() => {
    dispatch(detailsProduct(productId));
  }, [dispatch, productId]);

**productActions.js >> Here is my code, I got the product list from Redux store, But facing a problem when initializing product details. **


export const detailsProduct = (productId) => async (dispatch) => {
  dispatch({
    type: PRODUCT_DETAILS_REUEST,
    payload: productId,
  });
  try {
    const { data } = await Axios.get(`/api/products/${productId}`);
    dispatch({
      type: PRODUCT_DETAILS_SUCCESS,
      payload: data,
    });
  } catch (error) {
    dispatch({
      type: PRODUCT_DETAILS_FAIL,
      payload:
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message,
    });
  }
};

**productReducer.js >>> Here is my code, I got the product list from Redux store, But facing a problem when initializing product details. **

export const productDetailsReducer = (
  state = { product: {}, loading: true },
  action
) => {
  switch (action.type) {
    case PRODUCT_DETAILS_REUEST:
      return { loading: true };
    case PRODUCT_DETAILS_SUCCESS:
      return { loading: false, product: action.payload };
    case PRODUCT_DETAILS_FAIL:
      return { loading: false, error: action.payload };
    default:
      return state;
  }
};

**Store.js >>> Here is my code, I got the product list from Redux store, But facing a problem when initializing product details. **

import { applyMiddleware, combineReducers, compose, createStore } from "redux";
import thunk from "redux-thunk";
import { productDetailsReducer, productListReducer } from "./productReducers";

const initialState = {};

const reducer = combineReducers({
  productList: productListReducer,
  productDetils: productDetailsReducer,
});
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
  reducer,
  initialState,
  composeEnhancer(applyMiddleware(thunk))
);

export default store;

Solution

  • Seems like there is a typo here that may be causing issues.

    // productDetils in Store.js file
    const reducer = combineReducers({
      productList: productListReducer,
      productDetils: productDetailsReducer,
    });
    
    // productDetails in ProductDetailsScreen.js file
    const productDetails = useSelector((state) => state.productDetails);
    const { loading, error, product } = productDetails;
    

    Try correcting this careless error.

    Other than that, nothing seems to be out of the ordinary. Your initial state for productDetails in the form of state = { product: {}, loading: true } seems correct.