2016-03-10 2 views
0

пожалуйста, проверьте код последующий:Реагировать Native - SetState Предупреждение о втором крепления

componentDidMount() { 
    /* 
    * Add listener 
    * The User has search for a team 
    */ 
    teamStore.addChangeListener("SEARCH_TEAMS", this.updateTeams.bind(this)); 
} 

componentWillUnmount() { 
    /* 
    * Remove Listener and clear the Store 
    */ 
    teamStore.removeChangeListener("SEARCH_TEAMS", this.updateTeams); 
    teamStore.resetTeams(); 
} 

/* 
* The API has find some new teams 
* Update the state and show the new teams in the listview 
*/ 

updateTeams() { 
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}); 
    this.setState({dataSource: ds.cloneWithRows(teamStore.getAllTeams())}); 
} 

информации: SEARCH_TEAMSEvent срабатывает другой Component.

Если я визуализирую компонент в первый раз, все работает нормально. Но если я сую страницу и снова перейти на эту страницу, я получил это предупреждение:

Предупреждение: SetState (...) можно только обновить установленную или монтажный компонент ...

ответ

1

Вы не сделали правильно ясно ваш слушатель событий, поскольку были даны различные ссылки на функции. Это заставляет слушателя событий продолжать прослушивание, и в нем вызывается setState.

Вот исправление:

componentDidMount() { 
    // Save the function you want to listen with so you can remove it later 
    this.updateTeamsBound = this.updateTeams.bind(this); 
    teamStore.addChangeListener("SEARCH_TEAMS", this.updateTeamsBound); 
} 

componentWillUnmount() { 
    teamStore.removeChangeListener("SEARCH_TEAMS", this.updateTeamsBound); 
    teamStore.resetTeams(); 
} 
Смежные вопросы