简体   繁体   English

组件的孩子没有出现

[英]Children of component are not showing up

I'm trying to build a Flip card with ReactJS, that have inside 2 others components which are : Frontside and BackSide.我正在尝试使用 ReactJS 构建一个翻转卡片,其中包含另外两个组件:Frontside 和 BackSide。 These components should have children such as BackgroundCard or Sectioned Card.这些组件应该有子元素,例如 BackgroundCard 或 Sectioned Card。 When I test the component I'm not getting anything on the screen and there is no errors in the console!当我测试组件时,屏幕上没有显示任何内容,控制台中也没有错误!

FlipContent.js翻转内容.js

function FlipContent() {
    const [setFront, setFrontState] = useState(true);
    const [setBack, setBackState] = useState(false);
    const [setFlipped, setFlippedState] = useState("");


    function FlippingCard() {

        setFrontState(setFront === true ? false : true);
        setBackState(setBack === false ? true : false);
        setFlippedState(setFlipped === "" ? "flipped" : "");

    }



    return (

        <div className={`flip-content ${setFlipped}`} onClick={FlippingCard} >
            <div className="flip-content-container" style={{ cursor: "pointer" }}>
                {setFront ? <FrontSide></FrontSide> : null}
                {setBack ? <BackSide> </BackSide> : null}
            </div>
        </div>
    );

}

And For the FrontSide/BackSide same this as this code对于 FrontSide/BackSide 与此代码相同

function FrontSide({ children }) {

    return (

        <div className="flip-content-front">
            <div style={{ cursor: "pointer" }}>
                {children}
            </div>
        </div>
    );

}

and here how I'm trying to preview the component这里是我如何尝试预览组件

function FlipPreview() {
    return (
        <Column>
            <Row className={css(styles.title)} wrap flexGrow={1} horizontal="space-between" breakpoints={{ 768: 'column' }}>
                Accordion <br></br>
            </Row>
            <FlipContent>
                <FrontSide>
                    <CardBackgroundComponent title="Testing" image={image}></CardBackgroundComponent>
                </FrontSide>
                <BackSide>
                    <SectionedCardComponent
                        title="Notarum Black"
                        content="Powerful and reliable, this 15” HD laptop will not let you down. 256GB SSD storage, latest gen."
                        link=""
                        linkDescription="Add To Cart"
                    />
                </BackSide>
            </FlipContent>
        </Column>
    );
}

I think you have not inserted something inside both component FrontSide , BackSide我认为您没有在FrontSideBackSide两个组件中插入一些东西

   <div className={`flip-content ${setFlipped}`} onClick={FlippingCard} >
      <div className="flip-content-container" style={{ cursor: "pointer" }}>
        {setFront ? <FrontSide> It's front side </FrontSide> : null}
        {setBack ? <BackSide> It's back-side </BackSide> : null}
       </div>
    </div>

So in your component you are not rendering children.所以在你的组件中你没有渲染孩子。 So you need to update two things.所以你需要更新两件事。

1) Taking the props in the FlipContent component as shown below 1)取FlipContent组件中的props如下图

function FlipContent(props)

2) Use the props when rendering inside the component as shown below 2)在组件内部渲染时使用props如下图

 {setFront ? <FrontSide>{props.children}</FrontSide> : null}
  {setBack ? <BackSide>{props.children} </BackSide> : null} 

the problem is in second step is it will load all the props of children , so you need to render only the specific component.问题是在第二步是它会加载孩子的所有道具,所以你只需要渲染特定的组件。 See the below one看下面一张

Update更新

There are multiple ways to solve this one will list one by one解决方法有多种,一一列举

solution one解决方案一

By using the name prop of the children通过使用孩子的名字道具

function FlipContent(props) {
  const [view, setView] = useState("FrontSide");
  function FlippingCard() {
    setView(view === "FrontSide" ? "BackSide" : "FrontSide");
  }

  const component = React.Children.map(props.children, child => {
    if (view === child.type.name) {
      return child;
    }
  });

  return (
    <div className={`flip-content`} onClick={FlippingCard}>
      <div className="flip-content-container" style={{ cursor: "pointer" }}>
        {component}
      </div>
    </div>
  );
}

Working codesandbox工作代码沙盒

Solution Two解决方案二

Instead of adding statically the names can be driven from the prop, this can't handle same component multiple times不是静态添加名称可以从 prop 驱动,这不能多次处理相同的组件

function FlipContent(props) {
  const [view, setView] = useState(props.children[0].type.name);
  const ref = useRef(0);
  function FlippingCard() {
    if (props.children.length - 1 === ref.current) {
      ref.current = 0;
      setView(props.children[0].type.name);
      return;
    }
    setView(props.children[ref.current + 1].type.name);
    ref.current += 1;
  }
  let component = <span />;
  React.Children.forEach(props.children, child => {
    if (view === child.type.name) {
      component = child;
      return;
    }
  });

  return (
    <div className={`flip-content`} onClick={FlippingCard}>
      <div className="flip-content-container" style={{ cursor: "pointer" }}>
        {component}
      </div>
    </div>
  );
}

Working codesandbox工作代码沙盒

Solution three解决方案三

Rendering multiple components and in the same wrapper itself.渲染多个组件并在同一个包装器本身中。

function FlipContent(props) {
  const [component, setComponent] = useState(props.children[0]);
  const ref = useRef(0);
  function FlippingCard() {
    if (props.children.length - 1 === ref.current) {
      ref.current = 0;
      setComponent(props.children[0]);
      return;
    }
    setComponent(props.children[ref.current + 1]);
    ref.current += 1;
  }

  return (
    <div className={`flip-content`} onClick={FlippingCard}>
      <div className="flip-content-container" style={{ cursor: "pointer" }}>
        {component}
      </div>
    </div>
  );
}

Working codesandbox工作代码沙盒

I feel solution three is the simplest one and you have the scalable way.我觉得解决方案三是最简单的一种,而且您拥有可扩展的方式。

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

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