微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ReactJS中的类别和子类别

如何解决ReactJS中的类别和子类别

你好,我正在尝试使用材质ui制作侧边栏菜单

侧边栏菜单包含类别和子类别。

我有这样的代码

要做什么?

I take some categories data from my Laravel API,on each categories they can have an object of sub categories,and I collapse this to show the sub categories from the parent category

If I collapse one every one collapse,this is normal because they share the same state,but I don't kNow how to make one state for each parent categories

const categoriesList = this.state.categories.map((item,k) => {
            return (
                <React.Fragment key={k}>
                    <ListItem button onClick={this.handleClick}>
                        <ListItemText primary={item.label} />
                        {item.category !== null ? this.state.open ? <ExpandLess /> : <ExpandMore /> : null}
                    </ListItem>
                    {item.category !== null ? <>
                        <Collapse in={this.state.open} timeout="auto" unmountOnExit>
                            <List component="div" disablePadding>
                                {item.category.map((ite,l) => <ListItem key={l} button className={classes.nested}><ListItemText primary={ite.label} /></ListItem>)}
                            </List>
                        </Collapse>
                    </> : null}
                </React.Fragment>
            )
        })
``

解决方法

我建议您为每个类别创建一个组件。因此,您将拥有:

const categoriesList = this.state.categories.map((item,k) => {
    return <Category key={k} {...item} />;
});

在代码的其他地方,您将拥有另一个组件:

const Category = (props) => {
    const [open,setOpen] = useState(false);
    function handleClick(e) { ... }
    return (
        <React.Fragment>
            <ListItem button onClick={handleClick}>
                <ListItemText primary={props.label} />
                    {props.category !== null ? open ? <ExpandLess /> : <ExpandMore /> : null}
                </ListItem>
                {item.category !== null ? <>
                    <Collapse in={open} timeout="auto" unmountOnExit>
                        <List component="div" disablePadding>
                            {props.category.map((ite,l) => <ListItem key={l} button className={classes.nested}><ListItemText primary={ite.label} /></ListItem>)}
                        </List>
                    </Collapse>
                </> : null}
            </React.Fragment>
    );

}

然后状态包含在Category组件中,您可以使容器组件保持简单。

[note]我更喜欢函数组件,因此在这里使用了它,但是Class Component不会改变这个答案。

,

这是我带有钩子的父组件

first time I use hooks

import React,{useState} from 'react';
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
import ExpandLess from "@material-ui/icons/ExpandLess";
import ExpandMore from "@material-ui/icons/ExpandMore";
import Collapse from "@material-ui/core/Collapse";
import List from "@material-ui/core/List";


export function Category(props){
    const [open,setOpen] = useState(false);
    let handleClick = () => {
        setOpen(!open)
    }
    return (
        <React.Fragment key={props.id}>
            <ListItem button onClick={handleClick}>
                <ListItemText primary={props.categorie.label} />
                {props.category !== null ? open ? <ExpandLess /> : <ExpandMore /> : null}
            </ListItem>
            {props.category !== null ? <>
                <Collapse in={open} timeout="auto" unmountOnExit>
                    <List component="div" disablePadding>
                        {props.category.map((ite,l) => <ListItem key={l} button className={props.classes.nested}><ListItemText primary={ite.label} /></ListItem>)}
                    </List>
                </Collapse>
            </> : null}
        </React.Fragment>
    )
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。