Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type error extend is not a function in styled components

I'm trying to use styled-components but get the below error -

TypeError: CustomElement.extend is not a function

Code:-

import React, { Component } from 'react';
import './App.css';
import styled from 'styled-components';


const CustomElement = styled.div`
  color: green;
  font-size: 30px;
`

const BlueElement = CustomElement.extend`
  color: blue;
`

class App extends Component {
  render() {
    return (
      <div>
        <CustomElement>
          div
      </CustomElement>
        <BlueElement>
          div
      </BlueElement>
      </div>
    );
  }
}

export default App;
like image 653
user1050619 Avatar asked Oct 19 '18 15:10

user1050619


People also ask

How do I import font family into styled-components?

Now you have your fonts, place them in your app, for example in a folder called src/fonts . Next create a global style using styled-components. We'll call this file fontStyles. js and place it in the src/ file of our React app.

How can you apply dynamic styles to styled-components?

One way to dynamically change css properties with styled components is to insert a custom prop into your React component and access said property using the dollar sign and curly braces commonly used for template literals. Our current example is testing to see if our use pointer prop is true.

What is shouldForwardProp?

shouldForwardProp ( (prop: string) => bool [optional]): Indicates whether the prop should be forwarded to the Component .

What problem does styled-components solve?

It Solves CSS Specificity Problems Styled components eliminate specificity problems as it encapsulates CSS inside a component. This means you don't have to worry about class names clashing or your UI turning into a mess due to class name clashes.


1 Answers

Change

const BlueElement = CustomElement.extend`
  color: blue;
`

To

const BlueElement = styled(CustomElement)`
   color: blue;
`
like image 175
Hemadri Dasari Avatar answered Oct 10 '22 00:10

Hemadri Dasari