我正在構(gòu)建 MERN 電子商務(wù)應(yīng)用程序,并且在從 Redux Store 中提取數(shù)據(jù)時遇到了一個奇怪的 UI 錯誤。從一個產(chǎn)品頁面切換到另一個頁面后,會顯示并更新舊產(chǎn)品圖像。如您所見:https://imgur.com/iU9TxJr在那里你可以看到減速器的代碼:export const productDetailsReducer = ( state = { product: { reviews: [] } }, action) => { switch (action.type) { case PRODUCT_DETAILS_REQUEST: return { loading: true, ...state }; case PRODUCT_DETAILS_SUCCESS: return { loading: false, product: action.payload }; case PRODUCT_DETAILS_FAIL: return { loading: false, error: action.payload }; default: return state; }};還有行動代碼:export const listProductDetails = (id) => async (dispatch) => { try { dispatch({ type: PRODUCT_DETAILS_REQUEST }); const { data } = await axios.get(`/api/products/${id}`); 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, }); }};最后,有組件的代碼,我將 redux 存儲帶入頁面const ProductPage = ({ match }) => { const dispatch = useDispatch(); const productDetails = useSelector((state) => state.productDetails); const { loading, error, product } = productDetails; useEffect(() => { dispatch(listProductDetails(match.params.id)); }, [dispatch, match]); console.log(product); return ( <> {loading ? ( <Loader /> ) : error ? ( <Message variant="danger">{error}</Message> ) : ( <Row> <Col md={8}> <Image src={product.image} alt={product.name} fluid /> </Col>...rest of component's code....我知道甚至最好不要將 redux 用于單個產(chǎn)品,但我正在使用這個項目進(jìn)行練習(xí),而且我有點(diǎn)被困在這個項目上。
React-Redux UI 錯誤。圖像更新滯后
慕斯王
2023-05-11 14:23:13