-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add onResetStore method to client to register callbacks after resetStore #2812
Conversation
Generated by 🚫 dangerJS |
@peggyrayzis awesome! Do we have plans to document this? Would you be able to open a docs PR / issue or want to add it here? |
@jbaxleyiii just added docs for |
This should be part of client init options, as it is no action, just configuration
And it can be used to also do |
@ShockiTV If it's part of the configuration, you'll only be able to register one callback (or an array if we change it) at initialization. There could be some scenarios where you would want to register a callback dynamically within a component, so I think it makes sense to leave it as a method. |
@peggyrayzis It is 1 callback and you can provide your own function to put 20 side effects to it with custom branching so no array needed ofc. |
Also the Just 1 callback is enough and developer can manage to put 10 effects in async function. Not mentioning that |
@ShockiTV I do think we need to have a way to unregister callbacks, but the benefit of the array vs a single method is the easy of keeping the listeners close to what they would be used for. For instance, two immediate use cases for this callback are to rewrite to the cache after resetStore (for apollo-link-state) and to force rerender your UI (or portions of it) after a store reset. If you have it as a method with callbacks, then you can do something like this: import { withApollo } from "react-apollo";
export class Foo extends Component {
constructor(props) {
super(props);
this.unsubscribe = props.client.onResetStore(
() => this.setState({ reset: false }
);
this.state = { reset: false }
}
componentDidUnmount(){
this.unsubscribe()
}
render(){
return this.state.reset ? <div /> : <span />
}
}
export default withApollo(Foo); This does show where we need an unsubscribe hook so it can be cleaned up though |
Bump up bundlesize
61b011c
to
5ca98d9
Compare
Adding this functionality will allow us to register a callback to write defaults to the store after
client.resetStore
for apollo-link-state, fixing this issue.