How to delay UnityContext instantiation? + Best practices with redux question #240
Replies: 2 comments
-
This doesn't answer the rest of your question, but there are a few docs on this point (look at the detailed explanations): |
Beta Was this translation helpful? Give feedback.
-
I've currently got a working implementation which simply places it on the window, like |
Beta Was this translation helpful? Give feedback.
-
Hi Jeffrey! Thank you so much for your work on this fantastic library.
In the first example in the docs, the
UnityContext
object is instantiated as a globalconst
variable, and then used as a prop in the same file. But in my application, I don't want to create and load theUnityContext
object immediately, I want it to only happen in response to a specific Redux action.My current approach is to use a middleware to create the UnityContext object and add it to the Redux store. Then, the component which needs to render it will get it from the Redux state as a prop using
mapStateToProps()
. (I can explain this in more detail if this is too vague, just let me know.)This does work, but it requires putting a mutable object into the Redux store: the UnityContext object, which may be mutated elsewhere by its own methods. I'd rather use a different approach and keep Redux state fully immutable. I'm curious how would you approach this problem?
(The only other idea I had was to store it in
window.unityContext
or similar, so it becomes available in the global scope, but this also seems like a bad practice. In addition, I have not managed to get this working yet.)Finally, I would appreciate so much any insights anyone may have about my approach using Redux middleware... It's unclear to me what consequences may occur from placing a mutable object in a Redux store, as long as it's not being mutated from the reducers themselves, which are still pure functions in my approach. Could this have performance implications, or trigger unexpected re-renders?
Thank you again!
Beta Was this translation helpful? Give feedback.
All reactions