Hey folks! I'm diving into JavaScript for a web app and decided to skip using React because I find it a bit bloated with its dependencies and the state management can get confusing. Instead, I'm experimenting with Web Components and Custom Elements. I'm trying to design my component classes to have their own internal state, flexible construction with arbitrary data, and they should extend `HTMLElement` for proper DOM representation.
However, I keep running into a wall where the constructors seem too limiting. For example, when I try to create an instance of my custom class using `new`, I hit errors because `document.createElement` is what actually builds the custom elements in the browser, and it restricts how I can set things up.
Is there really no way around this constructor limitation? I'd love to hear your thoughts on how to still utilize class-based composition while getting around these roadblocks in the DOM setup.
2 Answers
Are you trying to create custom elements without calling `customElements.define()`? Because that's kinda essential. If you haven’t already, check out lit.js; it could shed some light on what you’re trying to achieve.
You might want to check out some lightweight frameworks like reef.js. They offer good insights into working with custom elements. Also, if you’re struggling with React's renders, it could be coming from things like improper use of useEffect. You can also get tools and extensions to debug state changes in React. By the way, for bundling, I recommend trying out Vite—it's super fast and has great support!
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String