- Notifications
You must be signed in to change notification settings - Fork54
Enables getting the current state#60
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
j-f1 commentedOct 13, 2018
Perhaps |
Ya, that is reasonable. I wanted it to be clear that this was a single snapshot/immutable point-in-time of the state, and not a reference to it that would update, but I don’t feel that strongly. |
j-f1 commentedOct 13, 2018
That’s the kind of thing that would be best described by documentation IMO. |
This is actually how state used to managed, but I changed it in#43. Tracking stateoutside of React can make things complicated when it comes to async/concurrent rendering. With this implementation we could run the risk of Can you describe your use case in a little more detail? I usually recommend reading state via React, and then syncing with external services in the commit phase ( classSubscriptionextendsReact.Component{componentDidMount(){this.updateExternalState();}componentDidUpdate(){this.updateExternalState();}// Updates some external (non-React) code with the// new value(s) read from state in Connected.syncWithExternalState=()=>{const{ value}=this.props;updateExternalState(value);};render(){returnnull;}}constConnected=()=>(<Consumerselect={[selectName]}>{name=><Subscriptionvalue={name}/>}</Consumer>); It's verbose, but I suspect this will get easier in the future as the React team works on this class of problems. |
kabriel commentedOct 15, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The key distinction here i believe is in the use of the word I understand the concern with exposing this value externally, which is why i thought calling it |
brianc commentedOct 25, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Love this library. It works amazingly well with typescript & makes doing simple fluxy apps so quick and straight-forward. fwiw I ran into this very recently. When the app starts up I run a example: // actions.jsasyncfunctionloadUser(){constres=awaitfetch('/current-user')constuser=awaitres.json()mutate(state=>(state.user=user))} Later I want to do another fetch somewhere else using the stored user information. I would have to pass the user into the action, but what I want is another action like this: // actions.js...asyncfunctiondoSomethingCoolWithCurrentUser(){const{ user}=getState()awaitfetch(`/users/${user.id}/something-cool')} but you can't do that... you can do this: asyncfunctiondoSomethingCoolWithCurrentUser(){letuserId;mutate(state=>(userId=state.user.id))awaitfetch(`/users/${userId}/something-cool')} but that is kinda gross. Also IIRC if you were to do something like: asyncfunctiondoSomethingCoolWithCurrentUser(){letuser;mutate(state=>(user=state.user))awaitfetch(`/users/${user.id}/something-cool')} it gets weird because It's vaguely similar to redux-thunk's I was thinking of forking & adding it to try it out but then see this PR is already open so I just wanted to throw on a little cheer for it. 👏 |
I need access to the current state outside of the render tree so I added a bit of code to store and retrieve it. Maybe others are running into the same requirement.