简体   繁体   English

如何将道具传递给循环内的样式组件

[英]How to pass props to styled component inside a loop

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { Container } from './styles';

import { MdContentCopy, MdGroup, MdPerson, MdMovie, MdSettings } from 'react-icons/md';

const items = [
    {
        route: '/',
        icon: <MdContentCopy />,
        title: 'Orders',
    },
    {
        route: '/customers',
        icon: <MdGroup />,
        title: 'Customers',
    },
    {
        route: '/movies',
        icon: <MdMovie />,
        title: 'Movies',
    },
    {
        route: '/settings',
        icon: <MdSettings />,
        title: 'Settings',
    },
    {
        route: '/Profile',
        icon: <MdPerson />,
        title: 'Profile',
    },
];

class ItemList extends Component {
    state = {
        active: false,
    };
    render() {
        const { open, history } = this.props;
        const pathName = history.location.pathname;

        return (
            <Container open={open} active={this.state.active}> // PASSING ACTIVE PROPS TO STYLED COMPONENT
                {items.map((item, index) => {
                    if (item.route === pathName) this.setState({ active: true }); // THIS THROWS AN ERROR BECAUSE TOO MANY RE-RENDERS
                    return (
                        <Link to={item.route} key={index}>
                            {item.icon}
                            <span>{item.title}</span>
                        </Link>
                    );
                })}
            </Container>
        );
    }
}

export default ItemList;

I am trying to pass active props to my styled component (Container) inside the loop.我正在尝试将活动道具传递给循环内的样式组件(容器)。 I tried it with setState to trigger a re-render because if I just assign a variable (let active = false and if the if statement is true then active = true) it won't re-render the component and active will always be false.我尝试使用 setState 来触发重新渲染,因为如果我只是分配一个变量(让 active = false 并且如果 if 语句为真则 active = true)它不会重新渲染组件并且 active 将始终为 false . But setState inside a loop makes a ton of re-renders and throws a depth exceeded error.但是循环内的 setState 会进行大量重新渲染并引发深度超出错误。 Any ideas of how I could do this?关于我如何做到这一点的任何想法?

No need to setup the state in this use case (use item.route === pathName instead of this.state.active), just pass the active value as true or false to component, here is revised class mentioned below.在这个用例中不需要设置状态(使用 item.route === pathName 而不是 this.state.active),只需将 active 值作为 true 或 false 传递给组件,这里是下面提到的修改类。

But in this use case matching one route will pass to the container as active= true.但在这个用例中,匹配一个路由将作为 active=true 传递给容器。

class ItemList extends Component {
render() {
    const { open, history } = this.props;
    const pathName = history.location.pathname;

    const isActive = items.filter(item => item.route === pathName).length > 0;

    return (
        <Container open={open} active={isActive}> // PASSING ACTIVE PROPS TO STYLED COMPONENT
            {items.map((item, index) => {
                return (
                    <Link to={item.route} key={index}>
                        {item.icon}
                        <span>{item.title}</span>
                    </Link>
                );
            })}
        </Container>
    );
}

} }

Just I have copied your render method changes the concept here.只是我复制了你的渲染方法在这里改变了概念。 Just I have checked the activeStatus in render method and pass it.只是我检查了 render 方法中的 activeStatus 并传递了它。 For any state change render will be called and that time it will remake the activeStatus.对于任何状态更改,都将调用渲染器,届时它将重新制作 activeStatus。

    render() {
    const { open, history } = this.props;
    const pathName = history.location.pathname;

    //code here to check the pathName
    let activeStatus = (items.filter(item => item.route == pathName) || []).length > 0 ? true : false;

    return (
      <Container open= { open } active = { activeStatus } > 
        {
          items.map((item, index) => {
            return (
              <Link to= { item.route } key = { index } >
                { item.icon }
                < span > { item.title } < /span>
                < /Link>
                        );
        })
  }
                </Container>
            );
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM