首页 > 解决方案 > 样式化的组件主题道具在生产中抛出错误但不是开发

问题描述

我有一个 Styled 组件按钮,它可能会或可能不会采用主题道具

  ${props =>
    props.theme &&
    props.theme.length > 0 &&
    buttonVariant(color.white, color[props.theme], color[props.theme])}

在本地主机上一切都很好,但是在生产中我得到很多类似的错误

Component 'styled.button' (.sc-cHGsZl) references the 'theme' prop in its styles but no theme was provided via prop or <ThemeProvider>. 

为什么会这样?

标签: reactjsstyled-components

解决方案


尝试使用 ThemeProvider 组件包装您的应用程序。这对我有用,不知道为什么。可能是因为 ThemeProvider 通过 ContextAPI 将主题传递给其他组件,提供了主题。


推荐阅读