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.

Compatibility with React.memo (React 16.6)

See original GitHub issue

I guess this is a mix between a bug report and a feature request.

Description:

React 16.6 introduces functional component memoization with React.memo.

Components wrapped in the React.memo HOC will behave similar to components that extend React.PureComponent or implement a custom shouldComponentUpdate method, to only re-render when its props change.

If a React.memo-ed component is used as a base for a new styled component (e.g. by using styled(MemoizedComponent)), this results in an error:

Error: Cannot create styled-component for component: [object Object].

The “original” component seems to be available in the type property of the memoized component, so using styled(MemoizedComponent.type) works, but will presumably circumvent the desired memoization behaviour.

Example:

import React from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";

const Button = ({ children, ...props }) => (
  <button {...props}>{children}</button>
);

const MemoButton = React.memo(Button);

// This will crash!
// const StyledMemoButton = styled(MemoButton)`
//   color: red;
// `;

// This works
const StyledMemoButton = styled(MemoButton.type)`
  color: red;
`;

Reproduction:

https://codesandbox.io/s/n40noyo040

Expected behaviour:

I think especially for presentational components that styled components often are, memoization behaviour could be beneficial, so it would be nice for this to work out of the box. If that’s not a desirable course of action or until it can be implemented, I think documenting that memoized components don’t work with styled-components out of the box and possible ways to mitigate this (e.g. using .type) would be nice.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
probablyupcommented, Oct 24, 2018

Will look into it this weekend

0reactions
brandon-pereiracommented, Mar 5, 2019

I’m still experiencing this issue in IE 11 (working in all other browsers), I’ve updated react and react-dom 16.8.4 and updated styled-components to 4.1.4-alpha.5 (latest beta). Can you please check this in IE 11 and confirm?

Adding the MemoButton.type fixed the issue for me in IE 11, but seems to defeat the purpose to have to add that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React v16.6.0: lazy, memo and contextType
Today we're releasing React 16.6 with a few new convenient features. ... the same with function components by wrapping them in React.memo ....
Read more >
React.memo() and Suspense/React.lazy() - Techiediaries
React.memo() is a Higher Order Component i.e a function that takes a React component as a parameter and returns another component. It's similar ......
Read more >
React.memo() vs. useMemo() - Bits and Pieces
It is a higher-order component that accepts another component as a prop. It will only render the component if there is any change...
Read more >
React.memo() vs. useMemo(): Major differences and use cases
Learn what memoization is, how it works in React, and why React has two different methods for memoization: React.memo() and useMemo().
Read more >
React.memo, useMemo, useCallback | Geek Culture - Medium
memo () as an alternative to functions. Since React 16.8, React team add React Hook so they add two Hooks useMemo , useCallback....
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