question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extending a styled component

See original GitHub issue

I have a question about the intended inheritance pattern for StyledComponents.

Say I have:

class MenuItem extends React.Component {
  render() { return <foo className={this.props.className} />; }
}
const StyledMenuItem = styled(MenuItem)`
  height: 10px;
`

I’d like to extend StyledMenuItem with a subclass, let’s say Settings

class Settings extends StyledMenuItem {
  render() { return <bar className={this.props.className} />; }
}
const StyledSettings = styled(Settings)`
  width: 10px;
`

But this pattern doesn’t seem to be functional, the render method of MenuItem is getting called rather than Settings. What’s the best practice for extending React methods as well as styling? Right now it seems like I need to do it in two steps, extending MenuItem to Settings, and then applying the MenuItem and Settings styles.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
k15acommented, Apr 10, 2017

Inheritance isn’t a good pattern in React. Read more about that here: https://facebook.github.io/react/docs/composition-vs-inheritance.html

Personally I would rewrite your solution like this:

const MenuItemWrapper = styled.div`
  height: 10px;
`;

class MenuItem extends React.Component {
    render() {
        return (
            <MenuItemWrapper>
                {this.props.children}
            </MenuItemWrapper>
        );
    }
}

const SettingsWrapper = styled.div`
  width: 500px;
`;

class Settings extends React.Component {
    render() {
        return (
            <SettingsWrapper>
                {this.props.settings.map(setting => (
                    <MenuItem>
                        {setting}
                    </MenuItem>
                ))}
            </SettingsWrapper>
        );
    }
}

Hopefully that’s what you want to accomplish 😉

0reactions
bthallioncommented, Apr 10, 2017

That’s helpful feedback, thanks for the advice. Time to do a little bit of refactoring

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to extend styled components? - Stack Overflow
You can just do like this: const FlexContainer = styled.div` display: flex; flex-direction: column; justify-content: flex-start; ...
Read more >
Basics - styled-components
styled -components utilises tagged template literals to style your components. It removes the mapping between components and styles. This means that when you're ......
Read more >
Extend Styling of Existing or Third Party React Component ...
If you wish to style your own existing (or any third-party) React component, then you can simply use the styled-components' styled() ...
Read more >
Start learn styled components!(2)-Extending Styles
You can through extending styles to finish same thing above and more beautiful, easier to maintain. The way to using extending styles is...
Read more >
How To Add Styles To React Components With Styled ...
As a React Front-End developer, you sometimes need to take an existing React component and apply extra styles to it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found