【问题标题】:Want to return all items when the page has been loaded想要在页面加载后返回所有项目
【发布时间】:2021-01-21 14:37:05
【问题描述】:

我有一个小问题。当我进入我的商店页面(显示我的所有产品)时,我需要单击尺寸过滤器以加载我的所有产品。那不是我想要的。我希望在加载页面时呈现所有产品。我需要在我的代码中添加/更改什么才能使其正常工作?

代码存储:

import React,{Component, useState,useEffect} from 'react'
import Product from '../components/product'
import Nav from '../components/nav'
import Footer from '../components/footer'
import Searchbar from '../components/searchbar'
import Filter from '../components/filter'
import { listProduct } from '../../actions/productActions'
import {useDispatch,useSelector} from 'react-redux'
import {Link} from 'react-router-dom'





 function Store(){

    const productList = useSelector(state=>state.productList)
    const{products,loading,error}=productList
    const dispatch = useDispatch()
    const [items, setItem] = useState(products)
   
    

    
    

    useEffect(()=> {
        dispatch(listProduct())
       setItem(products)

    }, [products])

    // handlefilter for sizes
    const  handleChange = (e) => {
   
           if(e.target.value === 'S'){           
            setItem(products.filter(item=> item.size === "S" ) ) 
           }
           else if(e.target.value === 'XS'){
            setItem(products.filter(item=> item.size === "XS" ) ) 
           }
           else if(e.target.value === 'M'){
             setItem(products.filter(item=> item.size === "M" ) ) 
           }
           else if(e.target.value === 'L'){
            setItem(products.filter(item=> item.size === "L" ) ) 
           }
           else if(e.target.value === 'XL'){
             setItem(products.filter(item=> item.size === "XL" ) ) 
              
           }
           else {
             setItem(products)
           }      
           
    }
   

    //handle filter for sorting 
    const handleSort = (e)=> {
        console.log(e.target.value)
    }

    //handle filter for searching




    

     
    return (
        
        <div>
        <Nav/>
        <div className="store">

           
            <div className="title">
                <h1>Store</h1>
            </div>
            <aside>
                <Searchbar/>
                
                <ul>
                    <h2>Categories</h2>
                    <li>Women</li>
                    <li>Men</li>
                    <li>Clothing</li>
                    <li>Equipment</li>
                 
                </ul>
            </aside>
           
           <Filter handleChange={handleChange || handleSort}/>
       
            
        
   
           
           <ul className="products">
                     
       {
           
           items.map(product=> (
             
              <li key={product.id} className="product">  
             <Link to={"/product/" + product.id}><div className="img" style={{background: `url(${product.img})`, backgroundSize: 'cover'}}></div></Link>
                {/* LOOK OUT FOR TYPOS IN ROUTIING  dont put':' after /, this only applies
                when routing because the ": " implies for a parameter
                In this case you can directly access product.id  */}
              <Link to={"/product/" +  product.id}><h1>{product.name}</h1></Link> 
                 <p> <small>€</small>{product.price}</p>
                 <div>size: {product.size}</div>
           
             {product.qty > 0 ? <div>  <div>{product.qty} left</div></div> : <div>out of stock</div> }   
            
          
             
             </li>    
                
           
                )
                
                )
       }
       </ul>

        
         
            
         
           
        </div>
        <Footer/>
        </div>
    )
   
   
}

export default Store



code filter: 

<select onChange={props.handleChange}/>

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    只需将setItem(products) 添加到您的useEffect

    有关详细信息,请参阅此答案[1],但您不能使用来自 useSelector 的值作为 useState 的默认值。

    [1]Should I pass useSelector to useState

    【讨论】:

    • 我按照你说的做了,但现在过滤器不起作用
    • handleChange 函数是否被调用?
    • 不,如果我叫它。它会给我错误:× TypeError: Cannot read property 'target' of undefined
    • 查看上面编辑的问题以获得更清晰的代码,我已经添加了你说的代码+过滤器组件
    • 我的意思是,当您单击过滤器时,handleChange 函数是否被调用,如果是,event.target.value 的值是您所期望的吗? Filter 组件不包括在这里,所以我不能告诉..
    【解决方案2】:

    如果handleChange 适合你,你应该试试:

    useEffect(()=> {
            dispatch(listProduct());
            handleChange({ target: { value: ''} });
    }, [])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 2021-01-09
      相关资源
      最近更新 更多