Uh oh!
There was an error while loading. Please reload this page.
Use setProperty when setting style properties - #9302
Conversation
sebmarkbage
commented
Mar 31, 2017
It seems like going down this route would inevitably involve a change from camelCase to hyphenation by convention. Probably including React Native then. |
trueadm
commented
Mar 31, 2017
@sebmarkbage I'm a fan of dropping the camelCase conventions if we can going forward (we can transform them for initial releases with warnings) for RN too. Keeping them closer to true "CSS" helps reduce friction. |
gaearon
commented
Mar 31, 2017
It’s more awkward to type though unless you provide some compile-time helper. Also more awkward to transform in JS. |
@gaearon We could do something nice at compile-time to help people here. We could also simply avoid passing an object literal too and let people pass in a block of |
Personally, I prefer the camelCase convention as its consistent with the DOM prop APIs like |
trueadm
commented
Mar 31, 2017
@aweary In my opinion, this has always been one of the harder things for newcomers to understand, especially |
Why not do the name conversion at the JSX compiler level? |
gaearon
commented
Apr 1, 2017
Because you can't know something is a style if it's passed around. |
aweary
commented
Apr 4, 2017
Can we consider accepting this without updating the public API for now? Assuming the cost of transforming the names is minimal (they're memoized so it should be in most cases). It would close #6411 |
We need to benchmark this. I don't think we'll want to take a significant hit. The memoized forms still needs to be looked up in a map which is not free. If we want to support #6411 a safer bet might be to just check if it starts with |
aweary
commented
Apr 4, 2017
@sebmarkbage I'll benchmark before we move any further. |
sophiebits
commented
Apr 7, 2017
I think for now it makes sense to call .setProperty for names starting with -- and leave everything else the same. |
aweary
commented
Apr 9, 2017
@spicyj@sebmarkbage I've updated it so that only properties starting with |
setProperty is faster in all/most modern browsers. It also lets us support CSS variables.
gaearon
commented
Apr 18, 2017
Have you verified this works in a browser? |
gaearon
left a comment
There was a problem hiding this comment.
Looks good to me but please verify it works in the browser.
@gaearon I did, I tested in in Chrome, Firefox and Safari on macOS and both styles with CSS variables and styles without were set as expected. I also tested in IE9-11 and Android 4 and verified the styles that didn't use CSS variables were being set as expected. |
sophiebits
commented
Apr 20, 2017
great, feel free to merge :) |
@aweary You could do the following to also support both camelCase and dash-cased styles. varstyle=props.style;for(...){if(nameinDOMStyleObject){DOMStyleObject[name]=style[name];}else{DOMStyleObject.setProperty(name,style[name]);}}Edit: though ^^ might not be the best for performance. |
* Use setProperty when setting style properties setProperty is faster in all/most modern browsers. It also lets us support CSS variables. * Only use setProperty when setting CSS variables * Add test to ensure setting CSS variables do not warn * Make this PR pretty again * Run fiber test script
Just keeping things updated. react#9398
Work in progress. Resolves#6411
Using
setPropertydoes incur the cost of parsing the style name assetPropertyrequires the hyphenated property name (background-colornotbackgroundColor). I'm also not sure if we want to special case IE given the performance benefits mentioned #6411 (comment)cc @trueadm@sebmarkbage@spicyj