DEV Community

Discussion on: Cousins playing nicely: Experimenting with NgRx Store and RTK Query

Collapse
 
markerikson profile image
Mark Erikson

Great post! It's really cool to see this sort of cross-framework integration going back and forth.

One quick note - it's important to handle unsubscribing from queries as well. RTKQ's React hooks do that automatically, but in other cases like this you'll have to do it yourself. Per github.com/rtk-incubator/rtk-query... , the approach might look like this in a React class component:

  componentDidMount() {
    const { id, getPost } = this.props;
    this.currentPromise = getPost(id);
  }

  componentDidUpdate(prevProps: PostDetailProps) {
    const { id, getPost } = this.props;
    if (id !== prevProps.id) {
      this.currentPromise?.unsubscribe()
      this.currentPromise = getPost(id);
    }
  }

  componentWillUnmount(){
    this.currentPromise?.unsubscribe()
  }
Enter fullscreen mode Exit fullscreen mode

Not an Angular or Rx expert, but I would assume that translates pretty straightforwardly into an NgRx usage setup.

Collapse
 
timsar2 profile image
timsar2 • Edited

Also how to configure http interceptor for fetch base query globaly?
Then it is posible to inject token and refresh token to each request.

Collapse
 
brandontroberts profile image
Brandon Roberts

Thanks! I updated the post and repo to reflect cleaning up the queries when the component is destroyed