代码之家  ›  专栏  ›  技术社区  ›  Jitender

Instance.GetChildContext不是函数

  •  0
  • Jitender  · 技术社区  · 6 年前

    当我升级时 style-compnent 2.1.2 4.0.2 . 我得到以下错误。我找不到其他方法来了解情况。如何传递传递上下文,如果当前人员不可能这样做,则如何返回 .dive 没有传递上下文。

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  0
  •   Slava Senchenko    6 年前

    关键是 styled-components 2.x.x支持react v0.14,但4.x.x切换到react v16.3。这是他们的一句话 doc :

    • v2.x(react v0.14+):ie9+,所有常青浏览器
    • v3.x(react v0.14+):ie9+,所有常青浏览器
    • v4.x(react v16.3+):ie11,ie 9+(带map+集合polyfill),所有常青浏览器

    react已经改变了它的上下文api,现在它没有 getChildContext() 返回上下文对象的方法。您可以阅读更多关于新的react上下文api的信息 here

    自从 ThemeProvider theme prop(这是将主题对象传递给它的方式),如果不想重写太多代码,只需更改 GetChildContext() props.theme . 否则,你最好用这种方法 推荐 通过 jest-styled-components 并按以下方式编写,将主题直接传递给组件:

    const wrapper = shallow(<Button theme={theme} />)
    

    例如,在哪里

    const Button = styled.button`
      color: ${props => props.theme.main};
    `
    
    const theme = {
      main: 'mediumseagreen',
    }