2 ms·
> react passes everything as attributes for some reason Because that was their design choice: if you write attributes you pass them as attributes to the compon
by troupo 3y ago
> react passes everything as attributes for some reason
Because that was their design choice: if you write attributes you pass them as attributes to the components
> I personally don’t understand why this hasn’t been fixed.
Because React is very popular and it's used everywhere in Facebook. You can't just change the behaviour without breaing a lot of stuff
> Preact has managed to support properties
> it would be fairly easy for React as well.
Yup, they should "just" do it. Meanwhile, here's an overview of what's needed for that support: https://github.com/facebook/react/issues/11347 https://github.com/facebook/react/issues/11347
Preact uses Option 2 in the list. Other libraries go for either Option 1 or Option 3. React is going with option 2 in an experimental branch.
- e12e 3y agoThank you. For others that are still confused (why would you care about attributes anyway?!) - I found this enlightening (and a little sad wrt how convoluted Dom/browsers still are): https://javascript.info/dom-attributes-and-properties https://javascript.info/dom-attributes-and-properties In particular: > So, if an attribute is non-standard, there won’t be a DOM-property for it. Is there a way to access such attributes? > Sure. All attributes are accessible by using the following methods: > elem.hasAttribute(name) – checks for existence. > elem.getAttribute(name) – gets the value. > elem.setAttribute(name, value) – sets the value. > elem.removeAttribute(name) – removes the attribute. > These methods operate exactly with what’s written in HTML.
- troupo 3y agoAnd web components complicate this further because they can expose both attributes and properties, and have code that reacts to change in both (`attributeChangedCallback` is a lifecycle method).