Hey guys,
I’ve got myself into a bit of a mess here.
I’ll briefly explain what I’m doing first.
I have a basic Sidebar, Main and Footer layout.
Links can be in the Sidebar or the Main areas but will all render into the Main component.
I’m using React Router v4 for all the routing and not using Blaze at all, everything is React.
I have basic user profile pages set up which display the users info and each profile page is accessed via a url like this : example.com/username.
In the UserProfile template I access the required user’s data based on the url parameter (/username).
This is how I am getting it in the component, setting the data to state.
constructor(props) {
super(props);
this.state = {
gotData: false,
profile: {
active: false
}
}
this.findUser = this.findUser.bind(this);
this.findUser(this.props.match.params.username);
}
componentWillUpdate() {
if (this.props.match.params.username) {
this.findUser(this.props.match.params.username);
}
}
componentWillUnmount() {
this.setState({
gotData: false,
profile: {
active: false
}
});
}
findUser(username) {
let local = this;
Meteor.call('findUserProfile', username, function(err, res){
if (res) {
local.setState({
gotData: true,
profile: {
active: true,
pid: res.pid,
username: res.username
}
});
} else {
local.setState({
gotData: true,
profile: {
active: false
}
});
}
});
}
The problem I’m having is the following :
When I move from a user profile link (/username) to a static page (/home) everything works as expected but I am getting the following error in the console :
Warning: Can't call setState (or forceUpdate) on an unmounted component.
This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
in UserProfile (created by ReactMeteorDataComponent)
in ReactMeteorDataComponent (created by Route)
If I remove the componentWillUpdate
function the error goes away but the user profile pages never update with new user data if the UserProfile component is already mounted. If you are coming from a non-profile url like (/home) which uses a different component they will work though.
So I need to use componentWillUpdate to ensure the user data is re-rendered if the user data has changed while the UserProfile component is mounted.
I understand why the error is being thrown (as it’s trying to set.State on the now unmounted component when navigating to a non UserProfile page) but I don’t know how to work around it in this instance. Any help greatly appreciated.