简体   繁体   English

如何在反应中设置嵌套组件的样式?

[英]How to style nested components in react?

I coded a table of content using nested components.我使用嵌套组件编写了一个目录。 Each component is a list of headers.每个组件都是一个标题列表。

I want to style each component with an indentation effect (margin-left: "20px") to differentiate each level of nesting.我想用缩进效果(margin-left:“20px”)为每个组件设置样式,以区分每个嵌套级别。

Example:例子:

<Parent> 
-->indent <Child/>
 -->indent   <Child2/>
   -->indent    (etc.)
</Parent>

Any idea of how to do it dynamically?知道如何动态地做到这一点吗?

Here's my code:这是我的代码:

import React from "react";

const TocContent = ({ props }) => {
    return (
        <div className="TOC">
            {props.TOC.map((header) => (
                <HeaderList key={header.objectId} header={header} props={props} />
            ))}
        </div>
    );
};

const HeaderList = ({ header, props }) => {
    return (
        <div>
            <li
                onMouseDown={(e) => e.stopPropagation()}
                className="listing"
                style={{}}
                onClick={(e) =>
                    props.handleHeaderClick(
                        header.level,
                        header.treepath,
                        header.containsLaw,
                        header.sections,
                        header.secNum,
                        header.objectId,
                        header.id,
                        e.stopPropagation(),
                    )
                }
            >
                {header._id}
            </li>
            {/* // if savedIndex === CurrentParent Index */}
            {props.headerIndex === header.objectId &&
                props.headers2.map((node2) => (
                    <HeaderList key={node2.objectId} header={node2} props={props} />
                ))}
            {props.headerIndex2 === header.objectId &&
                props.headers3.map((node3) => (
                    <HeaderList key={node3.objectId} header={node3} props={props} />
                ))}
            {props.headerIndex3 === header.objectId &&
                props.headers4.map((node4) => (
                    <HeaderList header={node4} key={node4.objectId} props={props} />
                ))}
        </div>
    );
};

export default TocContent;

Put the margin (or padding) on the element that contains both the HeaderList 's main content and the sub- HeaderList components (instead of just the main content as you have now).将边距(或填充)放在同时包含HeaderList的主要内容和子HeaderList组件的元素上(而不是像现在这样仅包含主要内容)。 Specifically this would be the div that wraps all other returned content in the HeaderList component.具体来说,这将是包含HeaderList组件中所有其他返回内容的div The margins will stack up and each nested header list will be more indented than the parent.边距将堆叠起来,每个嵌套的 header 列表将比父列表缩进更多。

For example (just HTML & CSS):例如(仅 HTML 和 CSS):

 .header-list { margin-left: 20px; }
 <div class="header-list"> First Element <div class="header-list"> Second Element <div class="header-list"> Third Element </div> </div> </div>

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

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