CharlesStover/reactn

Diff between useGlobal() and useGlobal('someProperty')

m4ttheweric opened this issue · 3 comments

I have some state stored using createProvider(). The state has quite a few properties, so I am making use of it by going with

const [state, setState] = MyProvider.useGlobal()

I have a component that needs a boolean to show/hide itself (it's a Material-UI dialog). I pass it the state.show property and when I update that property in another module using a set, it doesn't seem to open the dialog except when I do some other action to re-render the parent components. However, if instead I bring in the 'show' property like so:

const [show, setShow] = MyProvider.useGlobal('show')

Then everything works swimmingly.

Is there a difference between how a component listens to the state based on whether you use the entire state or just a property?

As far as subscriptions are concerned, global.show and useGlobal('show') should both cause the component to re-render when that value changes.

The only exception to this would be MyProvider.getGlobal(), as the getGlobal helper function is explicitly designed to not subscribe so that it can be used outside of components (e.g. in helper libraries).

I might need to see a minimal reproduction if possible, as this would be a bug as described. It may be that some other issue is causing it to appear like this is occurring. Here is the unit test that verifies that accessing a property on useGlobal()'s returned state should trigger a re-render when that property changes. If there is an edge case where this doesn't happen, I would like it to be identified, fixed, and tested as well. :)

Thanks for the quick reply @CharlesStover. I will try to drum up an example of this in the near future if I can, or report back if I am able to isolate what is happening. The odd thing is that the state is getting updated by the other component, but it takes some other action for the UI to re-render. So, it seems like it's not reacting to the state change unless another action happens first. It was pretty maddening to figure out and I started to doubt everything I thought I understood about your plugin and the laws of nature : )

Was this able to be reproduced?