我可以从状态中过滤出一组对象然后渲染它们吗?

时间:2019-02-15 18:06:55

标签: node.js reactjs redux react-redux

我正在尝试从状态中过滤产品,然后将其呈现在页面上。

这个想法是比较products.user_id并确保它与我当前登录的user.user_id匹配,然后仅呈现通过的产品。

我正在尝试在页面上显示一个视图,该视图总共包含所有产品,并且该视图旁边的视图将具有所有已登录用户产品的特定信息。

我尝试了一堆过滤器的不同组合,但是我认为主要的问题是找到如何在过滤器功能中使用渲染。

使用map时,您可以直接将不同的属性映射到页面上,并将它们作为要传递的道具向下传递,例如,使用filter时,它不会使用相同的语法。我可能是错的。

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { get_user, get_products } from '../../ducks/reducer';
import axios from 'axios';

class SellerProducts extends Component {
constructor() {
    super();
    this.state = {
        products: []
    };
}
componentDidMount() {
    this.props.get_user();
    // console.log('specific user product', this.props.get_products());

    axios.get('/api/product').then(res => {
        // console.log('Get all products (SellerProducts', res);
        this.setState({ products: res.data });
        console.log('SellerProducts products', this.state.products);
    });
    console.log('sellerproduct component mount user', this.props.user);
}
render() {
    let sellersItems = [];

    // if (this.state.products.length > 0) {
    //  sellersItems = this.state.products.filter((products, i) => {
    //      console.log('incoming array',products)
    //      return products[i].user_id === this.props.user.user_id;
    //  });
    // }
    if (this.props.loggedIn === false) {
        return (
            <div className="product_container">
                <div>
                    {sellersItems}
                    <div>
                        {this.props.product_name}
                    </div>
                    <div>
                        {this.props.info}
                    </div>
                    <div>
                        {this.props.product_type}
                    </div>
                    <div>
                        <img
                            className="sellerProductImages"
                            src={this.props.img_url}
                            alt={this.props.product_type}
                        />
                    </div>
                </div>
            </div>
        );
    }
    if (this.state.products.length > 0 && this.props.loggedin === true) {
        sellersItems = this.state.products.filter((products, i) => {
            console.log('incoming array', products);
            return products[i].user_id === this.props.user.user_id;
            return {
                products[i].user_id === this.props.user.user_id;

            } 

        });
    }
}
}
const mapStateToProps = state => state;

export default connect(mapStateToProps, {
get_user,
get_products
})(SellerProducts);

3 个答案:

答案 0 :(得分:1)

您没有在第二个块中退货 你也回来两次。在下面的方块中删除

   var sellersItems = this.state.products.filter((products, i) => { // <-----
        console.log('incoming array', products);
        return products[i].user_id === this.props.user.user_id;
    });

    return sellersItems.map(item => {
       // However you want to display the items
       return <div> {item.user_id} </div>
    })

答案 1 :(得分:0)

看来您已经很多了。没错,您不能直接从过滤器进行渲染,但是您生成了过滤后的列表,可以将其映射。

if (this.state.products.length > 0 && this.props.loggedin === true) {
    sellersItems = this.state.products.filter((products, i) => {
        console.log('incoming array', products);
        return products[i].user_id === this.props.user.user_id; 
    });
    sellersItems.map(item => (<span>{item.name}</span))
}

答案 2 :(得分:0)

您可以像这样在过滤器内创建一个jsx元素:

if (this.state.products.length > 0 && this.props.loggedin === true) {
    sellersItems = this.state.products.filter((products, i) => {
        console.log('incoming array', products);
     if( products[i].user_id === this.props.user.user_id ){
       return <span> { products[i].name } </span>
     } 
    });
}

这样,您只需遍历所有产品一次,而不是先过滤再映射,然后再进行单独的迭代。