[英]Redux is re-rendering a component when I don't want it to
I am using React and Redux for my website and have ran into an issue.我在我的网站上使用 React 和 Redux,但遇到了问题。 I have this card.我有这张卡。
I can click on the left/right button to see the previous/next image like this:我可以单击左/右按钮来查看上一张/下一张图像,如下所示: But Redux re-renders the component when I click on the +/- button to change the quantity of the product which causes the image to be changed to the original one.但是当我单击 +/- 按钮更改产品的数量时,Redux 会重新渲染组件,这会导致图像更改为原始图像。 What I want is to make the image stay as it is, but change the quantity at the same time.我想要的是让图像保持原样,但同时改变数量。
There is a lot of code for me to post here, so I will post it on GitHub.我这里有很多代码可以发,所以我会发到 GitHub 上。 This is the repo这是回购
I think the main files to look at are Shop.js
, ProductCard.js
, QuantityBox.js
and app/reducers/ProductSlice.js
我认为要查看的主要文件是Shop.js
、 ProductCard.js
、 QuantityBox.js
和app/reducers/ProductSlice.js
This is the code that is responsible for updating the state:这是负责更新状态的代码:
import { createSlice } from "@reduxjs/toolkit";
export const ProductSlice = createSlice({
name: "products",
initialState: [
{id: 1, name: "Apple Watch 5", description: "some description", images: ["/product_images/product1_image1.jpeg", "/product_images/product1_image2.jpeg", "/product_images/product1_image3.jpeg"], price: 450, quantity: 0},
{id: 2, name: "Apple Watch 5", description: "some description", images: ["/product_images/product2_image1.jpeg", "/product_images/product2_image2.jpeg", "/product_images/product2_image3.jpeg"], price: 475, quantity: 0},
{id: 3, name: "Apple Watch 5", description: "some description", images: ["/product_images/product3_image1.jpeg", "/product_images/product3_image2.jpeg", "/product_images/product3_image3.jpeg"], price: 500, quantity: 0}
],
reducers: {
incrementQuantity: (state, action) => {
const product_id = action.payload
const product = state.find(product => product.id === product_id)
if(product){
product.quantity++;
}
},
decrementQuantity: (state, action) => {
const product_id = action.payload
const product = state.find(product => product.id === product_id)
if(product && product.quantity > 0){
product.quantity--;
}
}
}
})
export const selectProducts = state => state.products
export const {incrementQuantity, decrementQuantity} = ProductSlice.actions;
export default ProductSlice.reducer
The problem is in your Shop
component.问题出在您的Shop
组件中。 Every time, Shop
rerenders, you assign new unique key
properties to your ProductCard
components.每次Shop
重新呈现时,您都会为您的ProductCard
组件分配新的唯一key
属性。 That means that React doesn't find the old one, throws away the old component tree and builds up a completely new one - and resets all component's state in the process.这意味着 React 不会找到旧的,丢弃旧的组件树并建立一个全新的组件树 - 并在此过程中重置所有组件的状态。
This is the code you have这是您拥有的代码
<div id = "product-card-grid">
{products.map(product => {
return <ProductCard key = {uniqid()} product_id = {product.id} images = {product.images}/>
})}
</div>
and instead you should be using stable key
props:相反,您应该使用稳定的key
道具:
<div id = "product-card-grid">
{products.map(product => {
return <ProductCard key = {product.id} product_id = {product.id} images = {product.images}/>
})}
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.