-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow registerStyle after render... #13
Comments
This should already be working with HMR, it used to work perfectly (haven't been using with React 0.14 yet). The feature you describe used to actually be well-documented and is still supported, but you can not update styles within a render loop (the React update model blocks this). If you want to do this, try using Edit: Let me know if this is the issue, of course. If it's broken, this is a bug. Edit 2: A better phrase would be "Inline styles can not be registered during |
Released a new minor version with the log still in place, but moved and no longer restricting a render (just note, it will crash React, but now it's transparent why). |
I'm also having trouble hot reloading styles using react-transform-hmr. I have a root component that renders the |
After a bit of debugging it seems that with react-transform-hmr |
@blakeembrey Thanks for making that change... wish it were possible, since I'd like to be able to use it for static methods... that said, if the message indicated one should use |
@blakeembrey Great! I probably should've opened another issue for this as it's not directly related to original topic, just saw HMR being mentioned :) Anyways, I created a sample case from react-transform-boilerplate: https://github.com/hoppula/react-free-style-hmr Editing I just tested the exact same setup with react-hot-loader and it works, so maybe I'll go with react-hot-loader for now, there's just that deprecation notice in react-hot-loader repo that makes me a bit worried about the future. |
@hoppula Thanks so much for this! Give me a bit of time to check it out, but that's good to see. You might be right about HMR, I believe the last time I was doing this was with |
@blakeembrey my thought was something like...
Essentially a static class that used/generated themes using freestyle against a themes property that is passed in... Using redux, themes can be changed/loaded/reloaded, so may change after the first render (which works) on a re-render, or in HMR. When the className is the same (from cache) it works... but if I update a component and HMR fires, or if my theme changes, it blows up with the parent error... If I can find time, I'll try and work up a simple demonstration, something similar to @hoppula above. I worked around the issue, for now by using inline styles where they are changing... I'm still pretty early on with a proof of concept at work, so didn't really dig into this. |
@tracker1 Sorry for the huge delay, a lot come up recently. Technically this currently is supported, but it's part of the context and requires you to register and remember to unregister. I'm going to figure out if I can make a helper method that hooks into the current elements render lifecycle (perhaps you know?). Right now I'm going to figure out why HMR isn't working with the shared demo - thank you for it! |
Is it possible that you can generate the theme hashes up front? The problem is you can't update an unrelated element within the same update loop. One workaround would be to ditch having React manage it and instead use the DOM node directly for updates. |
Looks related to gaearon/babel-plugin-react-transform#26 and gaearon/babel-plugin-react-transform#18. |
As an aside, it looks like neither works with the new stateless functional components (correct me if I'm wrong)? |
@blakeembrey I believe you are correct, in that neither works well for stateless/static component methods... My desire was to be able to work with edit/change and allow user selection of themes without having to reload the page... since the theme would be loaded via redux higher up, and passed in via props, it made sense to me... I plan to use mostly static (stateless) components. |
One option, is you could do something similar to what I am for managing my themes, in order to create a context for your <Style.Element /> ... As opposed to a style element component, you create a Style.Container ...
Any Child component only has to register that it needs It would invert the scope, but allow for safe changes at render...
I am actually doing something similar to pass down my theme... Would be nice, if you could have a "theme" property on the container component, that was also passed via context... as a convenience method. |
Thanks for the update. There's a few implications of doing this, but I'll keep it in mind. Here's my concerns though:
Of course, I could be reading the proposal wrong. I do think supporting this is a priority, would love a clean API. We can think about how to support this together. Here's an initial thought:
Does that sound correct? |
I'm just re-reading this, and it seems everything in #13 (comment) is actually how it worked in 2.0 but it wouldn't have been acceptable for the edge-cases. I'm looking at releasing a 3.0 that uses However, I believe the restriction for needing to render styles outside of the render loop still exists because React won't let me re-render the style element itself while there's other updates still occurring. With that said, what's a reasonable workaround for React? |
I lied above. Turns out stateless components didn't run into that issue. I've updated the docs and added some tests. See 97d56f7. |
Actually, does anyone have experience testing re-renders? I just realised the reason I didn't hit the issue locally is probably because it's only doing the one render loop and the |
This may sound weird, but I'd like to be able to support adding styles after the style.element is rendered. I currently get the following error.
However, it looks like
StyleElement
listens for event changes to force a rerender... so I should be able to register styles dynamically after the initial render. I understand this may be problematic for a server-side render, but I'd like to be able to change my themes dynamically after the application is loaded, and register themes as such.Not to mention, with HMR, I'd like to be able to edit styles on my component(s), this error forces me to refresh the entire application/page in order to see style changes.
The text was updated successfully, but these errors were encountered: