-
Notifications
You must be signed in to change notification settings - Fork 180
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
README Enhancement: Initial store state is by default an empty object #5
Comments
@nhardy Great idea. This pattern allows for the |
Now included in Advanced Usage docs. Thanks! |
Just an FYI. It appears that there is a race between initializing the store and having the first state sent to you. If you subscribe after the port connected, you won't receive the callback. |
@sb8244 Thanks for reporting this! Can you expand a bit more on the problem/solution? Having trouble picturing it for some reason. Maybe because it's Thursday. |
@tshaddix I believe he's saying:
For posterity: https://github.com/tshaddix/react-chrome-redux/blob/master/src/store/Store.js So, as @sb8244 had mentioned, the solution here would be to check
|
@vhmth Thanks for laying it out! So this store is supposed to act exactly like a Redux store. Does redux send state on subscriptions? I don't see anything here: https://github.com/reactjs/redux/blob/master/src/createStore.js#L101 |
Seems like they don't handle it on https://github.com/reactjs/react-redux/blob/master/src/components/connect.js#L197 I'm not sure where we want to place the separation of concerns, but my gut would be to leave it as is and have this lib behave more like |
I don't think that react-chrome-redux needs to handle this. I think that it is good to know about as a developer, but not something I would expect this to solve. The async nature of this redux store implementation inherently means things might need to be done differently. There seems to be a reasonable choice over keeping interface with a regular store, or customizing. I'm totally cool with library maintainers choosing interface over custom exceptions. FWIW:
Also, this is a great library. Thanks for the work y'all do on it. |
As @sb8244 mentioned the example in the documentation does not account for the case where the store has already initialized before the subscribing. Instead of fiddling around with subscribing and unsubscribing, we now take the declarative approach and watch the store, if it's empty we render an empty screen. Related: tshaddix/webext-redux#5 *: Hopefully. The issue was most commonly visible when upgrading Firefox which isn't something that's easy to test.
Something that isn't immediately obvious from the README is that the proxy
Store()
by default, will initialise thestate
as an empty object. This causes issues withreact-redux
if you are accessing nested properties in the state.Trawling through the code, however, https://github.com/tshaddix/react-chrome-redux/blob/b29d0e9b352bea1ac999415a73f0521eb63327f3/src/store/Store.js#L9 shows that there is an option to initialise a default state before this proxy Store receives state information from the wrapped Store. This is practical for small projects, and should be added to the README.
For larger, more complex initial states though, it may be more practical to wait until the proxy Store receives its initial state. See example below:
This example ensures that the App is only rendered upon receiving the initial state from the wrapped store. I think this would also be worth adding to the README.
The text was updated successfully, but these errors were encountered: