javascript - 对象作为具有样式化组件的 React 子项无效

标签 javascript css reactjs styled-components

我正在尝试渲染图像。这是我的代码:

Accordion .jsx

import React from 'react';
import ArrowTemplate from "./ArrowTemplate";

function Accordion() {
    return (
        <div>
            <ArrowTemplate arrowType={"BlackDown"} styles={""}/>
            {/*<ArrowTemplate arrowType={"BlackUp"}/>*/}
            {/*<ArrowTemplate arrowType={"WhiteDown"} styles={"background:black"}/>*/}
            {/*<ArrowTemplate arrowType={"WhiteUp"} styles={"background:black"}/>*/}
        </div>
    );
}

export default Accordion;

ArrowTemplate.jsx

import BlackDownArrowSVG from './svgs/black-down-arrow.svg';
import WhiteDownArrowSVG from './svgs/white-down-arrow.svg';
import styled from 'styled-components';
import PropTypes from 'prop-types';

ArrowTemplate.propTypes = {
    arrowType: PropTypes.string,
    styles: PropTypes.string,
};

function ArrowTemplate(props) {
    const {arrowType, styles} = props;
    switch (arrowType) {
        case "WhiteDown":
            return (
                styled.img.attrs({
                    src: WhiteDownArrowSVG,
                })`${styles !== null ? styles : ""}`
            );
        case "BlackDown":
            return (
                styled.img.attrs({
                    src: BlackDownArrowSVG,
                })`${styles !== null ? styles : ""}`
            );
        case "WhiteUp":
            return (
                styled.img.attrs({
                    src: WhiteDownArrowSVG,
                })`transform: rotate(180deg); ${styles !== null ? styles : ""}`
            );
        case "BlackUp":
            return (
                styled.img.attrs({
                    src: BlackDownArrowSVG,
                })`transform: rotate(180deg); ${styles !== null ? styles : ""}`
            );
        default:
            throw("You need to pass arrowType");
    }
}

export default ArrowTemplate;

SVG 路径正确。

作为我得到的错误:

Objects are not valid as a React child (found: object with keys {$$typeof, render, displayName, attrs, componentStyle, foldedComponentIds, styledComponentId, target, withComponent, warnTooManyClasses, toString}). If you meant to render a collection of children, use an array instead. in ArrowTemplate (at Accordion.jsx:7)

当我使用 console.log 时,我得到一个长对象。但是当我控制台记录来自 docs 的示例代码时,我得到了一个类似的对象。 :

const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

//^^^^^^^^^^^^^^^^^^^^^
//This renders with no problem

我哪里出错了?

最佳答案

如果您在声明带样式的组件时遇到这种情况,您可能忘记了声明末尾的模板文字。所以

const AppContentWithSideBar = styled((props) => {
  return (
    <Wrapper>
      <Router>
        <SideBarWrapper>
          <SideBar />
        </SideBarWrapper>
        <MainWrapper>
          {props.children}
        </MainWrapper>
      </Router>
    </Wrapper>
  );
});

应该是

const AppContentWithSideBar = styled((props) => {
  return (
    <Wrapper>
      <Router>
        <SideBarWrapper>
          <SideBar />
        </SideBarWrapper>
        <MainWrapper>
          {props.children}
        </MainWrapper>
      </Router>
    </Wrapper>
  );
})``;

关于javascript - 对象作为具有样式化组件的 React 子项无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57569814/

相关文章:

javascript - element.sortable 不是一个函数 Angular

javascript - 将 jQuery 对象存储到数组中以供以后使用

javascript - 地球的多边形三 Angular 剖分

html - 如何将 Material ui 选项卡移动到我的内容下方并将内容扩展到全高?

javascript - React-router(v4)否定双正斜杠

reactjs - 如何使用 ReactJS 在 MathJax 中换行

javascript - 动态添加带有 src 的脚本标签,其中可能包含 document.write

javascript - Div 不根据屏幕尺寸渲染

css - Algolia 自动完成在 Material 输入字段中不可见

javascript - 如何在 React Native 中进行搜索?