undefined context for dynamically added component
Hi team,
I am coding a toast component. The idea is to expose a
success
method via context. Users can pass in any components as the first param of the success
method to be displayed inside a toast.
Users can use ToastBody
as the first param, which will contain a close button. The close button will close the toast via onClose
method exposed in the context.
Using React, as demo here https://playcode.io/1290862, the console log context=...
prints out correctly the value of the context that the ToastBody is wrapped in.
Using Solid, https://playground.solidjs.com/anonymous/7b30d865-66e4-499e-b346-dbcab04670ba, The console log prints out undefined
Questions:
- I think this has something to do when the component is created. In Solid case, ToastBody
is created much after the ToastContext.Provider
is created, so it cannot set the correct owner. What do you think?
- What can I do to get the context in this case?Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
1 Reply
This is a very similar issue to this
https://discord.com/channels/722131463138705510/1084076207898898473/1084076207898898473
context needs to have access to owner object to lookup context
You might look into using
runWithOwner
Or making sure that the jsx is executed under a computation.
This is similar to how render
function accepts a function returning jsx, instead of jsx directly - it needs to be accessed under appropriate owner