react-helmet icon indicating copy to clipboard operation
react-helmet copied to clipboard

Uncaught RangeError: Maximum call stack size exceeded

Open dmitriykharchenko opened this issue 8 years ago • 45 comments

screenshot 2018-05-03 15 37 05

Hey, HelmetWrapper uses deepEqual to compare props and of course, there are react children components with endless amount of nested objects. This seems like a dangerous way to check props equality. Could we turn HelmetWrapper into PureComponent?

dmitriykharchenko avatar May 03 '18 22:05 dmitriykharchenko

Same here. Is there a solution to this?

srMarquinho avatar May 24 '18 10:05 srMarquinho

Yeh same.

The work around for us was to not use react children to pass in <meta> <title> tags, and use the props way:

<Helmet
  title={pageTitle}
  meta={[
  {
    name: 'description',
    content: pageDescription,
   },
   ]}
/>

willmcpo avatar May 29 '18 04:05 willmcpo

Just ran into this issue as well. Only saw it happening for Safari and Chrome browsers.

lhtdesignde avatar Jul 10 '18 08:07 lhtdesignde

What is the reasoning behind the deep equality check? Per the React docs...

We do not recommend doing deep equality checks or using JSON.stringify() in shouldComponentUpdate(). It is very inefficient and will harm performance.

nathanforce avatar Jul 11 '18 21:07 nathanforce

I believe you can also work around this by applying a unique key prop to the <Helmet> component, for example <Helmet key={window.location.href}>. Perhaps a bit cleaner than using the props method above.

jaredLunde avatar Jul 26 '18 05:07 jaredLunde

@jaredLunde not sure how that could speed up deepEqual in shouldComponentUpdate https://github.com/nfl/react-helmet/blob/c102586229549650ddb0f9615a42d351a2c7241b/src/Helmet.js#L93

dmitriykharchenko avatar Jul 26 '18 05:07 dmitriykharchenko

@dmitriykharchenko Sorry, that was a reference to the Uncaught RangeError: Maximum call stack size exceeded error and presumably https://github.com/nfl/react-helmet/issues/373#issuecomment-392650715 - not anything perf related.

jaredLunde avatar Jul 26 '18 05:07 jaredLunde

@jaredLunde this error happens because ReactHelmet compares props with deepEqual and when there are children in props it iterate over children as well and since those children are React components (not a plain objects) it simply overflows the stack. The workaround is to avoid providing any children to <Helmet/> component, but anyway, deepEqual is still bad.

dmitriykharchenko avatar Jul 26 '18 05:07 dmitriykharchenko

So, I don't see any reason why <Helmet/> needs a key prop

dmitriykharchenko avatar Jul 26 '18 05:07 dmitriykharchenko

@dmitriykharchenko Gotcha! Well in that case, key solves the problem because deepEqual never gets called in shouldComponentUpdate as the component re-mounts instead of updating. Hence, no stack overflow. Was very useful in my case.

jaredLunde avatar Jul 26 '18 05:07 jaredLunde

Hm, interesting. Do you have a link to read more about re-mounts of keyed elements?

dmitriykharchenko avatar Jul 26 '18 05:07 dmitriykharchenko

https://reactjs.org/docs/reconciliation.html#keys

Essentially, when the key changes it tells the reconciliation algorithm that the element in the new tree is different from the element in the old tree. If the new key wasn't in the children in old tree, a new component is mounted. If the new key was in the children in the old tree the element will be re-used.

jaredLunde avatar Jul 26 '18 06:07 jaredLunde

Aha, you mean that you have to update that key on every render. Yep, that seems faster than deepEqual, but still not quite clean as fix deepEqual or avoid children.

dmitriykharchenko avatar Jul 26 '18 06:07 dmitriykharchenko

Well, you only have to update the key when the underlying children change. In my case that happens when that particular view URL changes, but you can use any string representation. Maybe someone has a react-router match.params change which updates the title or meta information - that'd be a good spot to use key IMO. Anything where state affects the children.

jaredLunde avatar Jul 26 '18 06:07 jaredLunde

It might not work in some cases. Either way I think we're all in agreement that deepEqual is a bad practice in sCU.

jaredLunde avatar Jul 26 '18 06:07 jaredLunde

This is a really bad bug - as the props way is not even the documented recommended way to pass metadata to helmet, and there is no other workaround.

piotrblasiak avatar Aug 15 '18 10:08 piotrblasiak

Just replace it by https://github.com/FormidableLabs/react-fast-compare

theKashey avatar Aug 31 '18 13:08 theKashey

Why this is still not adressed?

ivan-kleshnin avatar Nov 05 '18 09:11 ivan-kleshnin

  1. key trick doesn't help in all cases
  2. Library still crashes in 5.2.0

This bug is devastating. Had to switch to react-helmet-async which doesn't have this problem 😢

ivan-kleshnin avatar Nov 23 '18 11:11 ivan-kleshnin

https://github.com/nfl/react-helmet/pull/402 was merged into master. I would like to get the roll-up PR merged and we can release a beta version for feedback.

tmbtech avatar Nov 25 '18 04:11 tmbtech