-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Enables getting the current state #60
base: master
Are you sure you want to change the base?
Conversation
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. |
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 state outside 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 ( class Subscription extends React.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() {
return null;
}
}
const Connected = () => (
<Consumer select={[selectName]}>
{name => <Subscription value={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. |
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 |
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.js
async function loadUser() {
const res = await fetch('/current-user')
const user = await res.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
...
async function doSomethingCoolWithCurrentUser() {
const { user } = getState()
await fetch(`/users/${user.id}/something-cool')
} but you can't do that... you can do this: async function doSomethingCoolWithCurrentUser() {
let userId;
mutate(state => (userId = state.user.id))
await fetch(`/users/${userId}/something-cool')
} but that is kinda gross. Also IIRC if you were to do something like: async function doSomethingCoolWithCurrentUser() {
let user;
mutate(state => (user = state.user))
await fetch(`/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.