最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin13浏览0评论

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

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

我正在尝试从我的州过滤产品,然后在页面上呈现它们。

我们的想法是比较products.user_id并确保它与我当前登录的user.user_id匹配,然后只渲染通过的产品。

我正在尝试在页面上有一个视图,其中包含所有产品,并且旁边的视图将包含所有特定登录用户的产品。

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

使用map,您可以直接将不同的属性映射到页面上并将它们作为道具传递下来,例如要渲染,但是使用过滤器,它不会使用相同的语法。我可能错了。

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);
回答如下:

您没有返回第二个区块中的项目。您还要返回两次。删除在下面的块中

   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>
    })
发布评论

评论列表(0)

  1. 暂无评论