我有一个 Main.js 组件,该组件可以路由到各种组件,包括 Listing.js 。
主要:它包含添加到购物车中的一系列产品作为状态。
列表:这是产品列表。它包含数据库中所有产品的状态。
我的问题:当我通过单击“列表”组件中的按钮将产品添加到购物车时,它将产品添加到购物车,更新了购物车的状态购物车 主要组件。这样,列表组件将重新呈现,并且我松散了访客在列表上设置的所有过滤器。
我想防止List在其父组件的购物车状态更改时重新呈现。您是否知道该怎么做?
Main.js (父组件)
import React from 'react';
import {Switch, Route, withRouter, Link} from "react-router-dom";
import {List} from "./Listing/List";
class Main extends React.Component
{
state={
cart:[],
};
removeFromCart = product => //REMOVES PRODUCT FROM CART
{
let cart = this.state.cart;
cart.map(item => {
if(item._id === product._id)
{
item.count--;
return item;
}
});
cart = cart.filter(item => item.count > 0);
this.setState({cart:cart}, () => {sessionStorage.setItem('cart', JSON.stringify(cart));});
};
addToCart = product => //ADD PRODUCT TO CART
{
let cart = this.state.cart;
let productExists = this.state.cart.map(item => {return item._id === product._id}).includes(true);
if(productExists)
{
cart = cart.map(item => {
if(item._id === product._id)
{
item.count++;
return item;
}
else
{
return item;
}
});
}
else
{
product.count = 1;
cart.push(product);
}
this.setState({cart: cart}, () => {sessionStorage.setItem('cart', JSON.stringify(cart));});
};
componentWillMount()
{
if(sessionStorage.getItem('cart')) this.setState({cart:JSON.parse(sessionStorage.getItem('cart'))});
}
render() {
return (
<div className='main'>
<Header cart={this.state.cart} />
<Switch>
<Route path='/listing' component={() => <List addToCart={this.addToCart} />} />
</Switch>
</div>
);
}
}
export default withRouter(Main);
List.js ,产品列表:
import React from "react";
import {Product} from "./Product";
import Data from '../../Utils/Data';
import {Search} from "./Search/Search";
export class List extends React.Component
{
state = {
serie: '',
products: [],
filteredProducts: [],
};
addToCart = this.props.addToCart;
obtainProducts = (request = {}) => //searches for products in database
{
Data.products.obtain(request).then(products => {
this.setState({products:products, filteredProducts: products});
});
};
displayProducts = () => {
//Only products that has title
const products = this.state.filteredProducts.filter(product => {return product.title;});
//Returns REACT COMPONENT
return products.map(product => {
return <Product
key={product._id}
product={product}
addToCart={this.addToCart}
/>
});
};
searchHandler = (collection, types) =>
{
let filteredProducts = this.state.products;
if(collection.length)
filteredProducts = filteredProducts.filter(product => {return collection.includes(product.series);});
if(types.length)
filteredProducts = filteredProducts.filter(product => {return types.includes(product.type);});
this.setState({filteredProducts: filteredProducts});
};
componentWillMount()
{
//init products collection
this.obtainProducts();
}
render()
{
const productComponents = this.displayProducts();
console.log('test');
return(
<section className='listing'>
<Search searchHandler={this.searchHandler} />
<div className='listing-content grid-4 has-gutter'>
{productComponents}
</div>
</section>
)
}
}
答案 0 :(得分:2)
移动 filter.State 从过滤器组件到主要组件或使用cookie
答案 1 :(得分:1)
您可以使用componentDidMount方法代替componentWillMount:
componentDidMount()
{
//init products collection
this.obtainProducts();
}
答案 2 :(得分:0)
您可以使用componentShouldUpdate()来防止这种情况。