-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
React Context not available in components #52
Comments
Without having had a deeper look into this: Did you try and compare the React Sample Project? It does not utilize the Webpack adapter, but also uses the Context API https://github.com/dennisreimann/uiengine-sample-react/blob/master/lib/uiengine/react.js |
Well, as far as I can tell, the Webpack adapter doesn't offer a similar hook like // react-server-render.js
module.exports = function serverRender(Component, props) {
return renderToString(
<ThemeProvider>
<Component {...props} />
</ThemeProvider>
)
} Not sure if that's the best way but it did insert the |
Yep, I have tried several ways now, but I also can't get the |
Damn!
Does that mean you were able to access the |
It is defined when you initialize it with Had another look earlier today and my guess is that it boils down to the same problem as mentioned in #46, namely that somehow stuff specified in the render files is not accessible in the variants, because they are two separate files and not bundled together. You can see this when you are using the latest versions (2.4.x) and have a look at how they are build (dist/_webpack) and integrated in the HTML. Don‘t know for sure, but that‘s what came to my mind as the most likely thing to cause this. |
I don't quite understand this point. Unfortunately, my webpack knowledge is kinda limited. |
That's correct. Nevertheless, currently the variants and render functions ( See the source of this example for instance: <script src="/_webpack/jsx-client.js"></script>
<script src="/_webpack/src_elements_label_variants_label_jsx-client.js"></script>
<script>
window.UIengineWebpack_render(
window.UIengineWebpack_component,
{"id":"name","title":"Name"}
)
</script> This is the generated output:
They aren't bundled together and from what I have seen so far, this is the problem with the React hooks and context APIs. They seem to rely on side effects that require both files to share the same scope (and by that the exact same React instance). I'll tinker more with that when I find the time to do so :) |
No worries, we'll figure out a way that works for our use case. :) |
I got it kinda working – at least on the client-side. Same problem still when rendering server-side, which I don't understand yet as the React instances there are the same too. However, as I am still unsatisfied with the approach I'm taking to make this work and I am still trying to find a better way, here is something I can imagine to work for you in the meantime: Use the Webpack externals option to extract React and ReactDOM from the bundle: externals: {
react: 'React',
'react-dom' : 'ReactDOM',
} You would then have to provide them via CDN or local linking in the preview uiengine.html template. |
Hey Dennis, Unfortunately, accessing |
Ok, took another look and I really don't know enough about how this is supposed to work to fix it. From what I saw the problem seems to be that the context cannot be shared across React instances coming from different bundles. |
Status update: I got it working now :) Will clean up the code and test whether or not this helps with the hooks issue as well during the next days. |
Closes #52. Signed-off-by: Dennis Reimann <mail@dennisreimann.de> new Signed-off-by: Dennis Reimann <mail@dennisreimann.de> new Signed-off-by: Dennis Reimann <mail@dennisreimann.de>
See the example here: https://uiengine-sample-project.uix.space/testcases/examples/ReactContext/ |
v2.5 is available now and fixes this :) |
Hey Dennis!
Sorry for this neverending story, but I have another problem using the jsx/webpack adapter. :/
Describe the bug
We have a use case where we need data available to every component. To avoid "prop drilling" we want to make use of the Context-API for this.
I tried to make use of the
![Bildschirmfoto 2019-08-10 um 15 42 59](https://user-images.githubusercontent.com/17713179/62822628-6f5eb400-bb86-11e9-9949-7cdf8a02d915.png)
react-server-render
andreact-client-render
for this to make the context available to every component/variant in UIEngine.This looked good at first, since the Context-Provider showed up in the component tree:
Unfortunately, the actual context is not accessible in the components of the UIEngine. It appears like the context is not passed through to all the underlying components.
To Reproduce
Steps to reproduce the behavior:
render()
method in<Footer />
Expected behavior
The context should be available in the
<Footer />
component.I can confirm that the same code works in CRA/Next.js.
Kind regards,
René
The text was updated successfully, but these errors were encountered: