WebReflection / heresy

React-like Custom Elements via V1 API builtin extends.
https://medium.com/@WebReflection/any-holy-grail-for-web-components-c3d4973f3f3f
ISC License
276 stars 16 forks source link

In Safari render() being run before the constructor() resulting in this.refs failing with undefined value as useRef never called #25

Closed AhnafCodes closed 4 years ago

AhnafCodes commented 4 years ago

https://codepen.io/ahnafcodes/pen/PooyGdK

Steps:

  1. please open the above codepen in Safari browser and in chrome. It works in chrome and fails to render in safari.
  2. Please open debugger in chrome and safari
  3. Reload each browser to see the difference. safariRefsFailing

Behavior: I have set debugger in "constructor" and "render" as you can see in codepen, oddly in the safari browser the render() runs before constructor().

It might be a custom component api issue in Safari, Unfortunately Safari is the default browser for Ipad users.

AhnafCodes commented 4 years ago

Does this issue stem from this https://github.com/wikipathways/pvjs/issues/408

AhnafCodes commented 4 years ago

I don't want to do this in render() when using any hooks:


if(isSafari && !this.refs) { 
         this.constructor(); 
      } 
// looks hacky and is ugly```
WebReflection commented 4 years ago

Will have a look once I get a chance to be in front of a PC (hopefully soon)

WebReflection commented 4 years ago

I think the issue is that you are using constructor which is forbidden in Safari ... use oninit event/method to define once anything you need, including refs.

TL;DR move anything you do in constructor within oninit() instead 😉