4
votes

I just installed the styled-components library and started playing with it.

However this simple example:

import React from 'react'
import styled from 'styled-components'

const Div = styled.div`background: red;`

const AlertCard = () => (
  <Div>Hello World</Div>
)
export default AlertCard

And the render function:

render() {
  const { fetchingAlerts, alerts } = this.props

  return (
    fetchingAlerts ? 
      <Loader /> :
      <div>
        <AlertFormContainer />
        <div>
          {alerts.length === 0 ?
            <EmptyAlerts /> :
            <div>
              {alerts.map(alert => (
                <AlertCard text="My text " />
              ))}
            </div>
          }
        </div>
      </div>
    )
  )
}

triggers this error: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

The styled component returns an object of $$typeof: Symbol(react.element)

I don't know what goes wrong

1
How do you integrate this component ? I tested with a create-react-app, and i have no problem. - Deve
Yep, same here... It's working. - Marc M.
@Deve This component is integrated in another view based on a map of results, but even when I want to render it alone, it throws the same error. The project has not been built using creat-react-app but the webpacker gem instead - Vincent Rolea
We will need your render, cause here, component works well. Problem should come from somewhere else. :/ - Deve
@Dave, thanks for taking the time. I've updated the question with the actual render function (even though the dumbest render with a single styled component does not work) - Vincent Rolea

1 Answers

4
votes

Which version of React, React-Dom and Styled-Component are you using?

Make sure that react and react-dom are >=16.3.0 with styled-component >=4.0.0

The breaking change brought upong the use of the new Context api in styled-component v4 requires react >=16.3.0

see point #2 on https://www.styled-components.com/docs/faqs#what-do-i-need-to-do-to-migrate-to-v4