3 ms·
React really should be able to detect object literals in style components and optimize those away into a const under the hood. Hopefully this is fixed in a fut
by tarr11 9y ago
React really should be able to detect object literals in style components and optimize those away into a const under the hood. Hopefully this is fixed in a future version.
- blitmap 9y agoI want to say you can 'hint' this by Object.freeze()'ing it.
- acemarke 9y agoReact doesn't do any "compiling". It simply takes the JS object tree that _you_ generated in your component's render function, and works from there. So yes, if you are declaring object literals in your render method, that will be a new object each time the component re-renders. If you know that a given style object is never going to change, you can hoist it out of your component's render method into the module scope yourself.
- tarr11 9y agoThis babel preset might do it. I haven't tried but it looks promising... https://babeljs.io/docs/plugins/transform-react-constant-elements/ https://babeljs.io/docs/plugins/transform-react-constant-ele...