Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should unsubscribe be handled in a react component when using redux?

In my component I have the following:

componentWillMount: function () {
  this.unsubscribe = store.subscribe(function () {
    this.setState({message: store.getState().authentication.message});
  }.bind(this));
},

componentWillUnmount: function () {
  this.unsubscribe();
},

Not calling unsubscribe causes the following error:

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.

What I'd like to know is should I be assigning unsubscribe to this or is there a better place to assign it?

like image 937
Clarkie Avatar asked Sep 03 '15 10:09

Clarkie


People also ask

How do I unsubscribe from component react?

And if you want to unsubscribe an action from the current component, this can be done as shown below. Include this. unsubscribe() because once you subscribe to the store, it returns the unregister function literally, which is used to unsubscribe the actions and stores once component gets unloaded.

In which lifecycle method should you unsubscribe from your real time updates listener?

Every time you call onSnapshot() the return value is a handler you can invoke to unsubscribe that listener later.

Where you would unsubscribe from a subscription in useEffect?

This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. The instruction is pretty clear and straightforward, "cancel all subscriptions and asynchronous tasks in a useEffect cleanup function".

How do I stop re rendering in react redux?

Options for preventing re-renders One popular method for preventing re-renders is using Selectors in React Redux, which are functions that subscribe to the Redux store and run whenever an action is dispatched. Selectors use === as a strict quality check, re-rendering the component whenever data is changed.


1 Answers

As mentioned by Salehen Rahman above in the comments I did end up using react-redux.

Following their documentation I created two functions, one to map the 'global state' to the props within the component:

function mapStateToProps(state) {
  return {
    users: state.users.items
  };
}

and one to map the dispatched actions to functions passed into the component as props:

function mapDispatchToProps(dispatch) {
  return {
    lockUser: (id) => dispatch(actions.lockUser(id)),
    unlockUser: (id) => dispatch(actions.unlockUser(id)),
    updateUser: (id, user) => dispatch(actions.updateUser(id, user)),
    addUser: (user) => dispatch(actions.addUser(user))
  };
}

This then all gets pulled together using the connect method:

export default connect(mapStateToProps, mapDispatchToProps)(UsersContainer);

I have a feeling that all this does under the hood is attach the unsubscribe method to the component but it does simplify things substantially.

like image 193
Clarkie Avatar answered Oct 04 '22 10:10

Clarkie