【发布时间】:2020-07-15 21:05:47
【问题描述】:
我有一些使用样式化组件的非常基本的代码:
import styled from 'styled-components';
const Test = () => <div>foo</div>;
const StyledTest = styled(Test)`
border: 10px solid red;
`;
console.log(typeof StyledTest); // object!!!
问题是,最后一个console.log 行记录了object(不是function),当我尝试使用它时当然会导致错误。
当我尝试为库中的组件设置样式时也会发生这种情况:
import Modal from 'react-modal';
const StyledModal = styled(ReactModalAdapter)`
border: 10px solid red;
`;
console.log(typeof StyledModal); // object!!!
我做错了什么?
【问题讨论】:
-
虽然这里的答案回答了我的问题,但对于遇到我遇到的问题的任何人(即,如果您收到关于您的
StyledTest-equivalent 不是函数/类的错误),答案很可能是您的 React/Styled Components 版本不同步。见:stackoverflow.com/questions/52973213/…