简体   繁体   English

React Native:在Render中调用的函数,循环运行

[英]React Native: function called in Render, running in loop

This is my code : 这是我的代码:

  "use strict"; import React, {Component, PropTypes} from 'react'; import { ActivityIndicator, ListView, StyleSheet, Text, TextInput, View, TouchableOpacity, } from 'react-native'; import { Actions, ActionConst } from 'react-native-router-flux'; var SQLite = require('react-native-sqlite-storage'); var DeviceInfo = require('react-native-device-info'); import Icon from 'react-native-vector-icons/Ionicons'; var LOADING = {}; var db = SQLite.openDatabase({name : "oc.db", location: 'default'}); class Profile extends Component { constructor(props) { super(props); this.state = { users: [], dataSource: new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2, }), }; } fetch(){ console.log('fetching data from database'); ////// var query = "SELECT * FROM users"; var params = []; var userlist = []; db.transaction((tx) => { tx.executeSql(query,params, (tx, results) => { var len = results.rows.length; for(var ind = 0; ind < len; ind++ ){ userlist[ind] = { userId: results.rows.item(ind).userId, userName: results.rows.item(ind).userName, userMail: results.rows.item(ind).userMail, active: results.rows.item(ind).active }; } this.setState({ users: userlist, }); }, function(){ console.log('Profile: Something went wrong'); }); }); ////////// } componentDidMount() { this.fetch(); } activate(uid){ console.log('Lets de-activate all accounts'); var query = "UPDATE users SET active='no' WHERE active=?"; var params = ['yes']; db.transaction((tx) => { tx.executeSql(query,params, (tx, results) => { console.log('all users should be de-active now'); console.log('activating user ', uid); var query = "UPDATE users SET active='yes' WHERE userId=? LIMIT 1;"; var params = [uid]; db.transaction((tx) => { tx.executeSql(query,params, (tx, results) => { console.log('UserId ' , uid , ' is Active now'); this.renderUsers(); //Actions.bridge({type: 'reset', toGo: 'home'}); }, function(){ console.log('No such a user found to make active'); }); }); }, function(){ console.log('No such a user found to make active'); }); }); } resetNav(){ Actions.bridge({type: 'reset'}); } renderUsers(){ console.log('Rendering Users'); //this.fetch(); var ulist = this.state.users; var urow = []; for(let a=0; a < ulist.length; a++){ let active = ulist[a].active === 'yes' ? '◉' : '◯'; let usid = ulist[a].userId; urow.push( <TouchableOpacity key={a} onPress={() => { this.activate(usid); /* this.resetNav(); */ }} style={styles.uitem}> <Text style={styles.uText}>{ulist[a].userName} {ulist[a].userMail}</Text> <Text style={styles.uTextAct}>{active}</Text> </TouchableOpacity> ); } return( <View style={styles.ulist}>{urow}</View> ); } render() { return ( <View style={styles.container}> <Text style={styles.pic}> <Icon name="ios-person" color="#00a2dd" size={180}></Icon> </Text> <Text style={styles.heading}> Welcome to your Profile page!{'\\n'} </Text> <Text style={styles.listtitle}>Users List</Text> <Text style={styles.listdesc}>To switch between accounts, simply click on them.</Text> {this.renderUsers()} </View> ); } } 

Results are shown, it's working. 结果显示出来,正在运行。 but problem is renderUsers() is running in a loop. 但是问题是renderUsers()正在循环运行。 it's being called forever. 它被永远呼唤。 where am i wrong? 我哪里错了? any solutions please? 有什么解决办法吗?


Update : When i put this.fetch to renderUsers() , it's fine. 更新:当我把this.fetch放到renderUsers()时,就可以了。 when you select a user, it will be active and you can see the list is updating. 选择用户后,该用户将处于活动状态,您可以看到列表正在更新。 but problem is list being updated in a loop. 但问题是列表正在循环更新。 it's happening ever and ever again. 它正在不断发生。

when i put this.fetch to componentdidmount , the loop problem is fixed, but when you change active user, list is not being updated. 当我将this.fetch放入componentdidmount时,循环问题已解决,但是当您更改活动用户时,列表未更新。

I found the solution. 我找到了解决方案。 in activate function, i added the Actions.refresh method from router-flux and i added this.setState in componentwillreceiveprops. 在激活功能中,我从router-flux添加了Actions.refresh方法,并在componentwillreceiveprops中添加了this.setState。 now it's working as expected. 现在它正在按预期工作。 final code is like this : 最终的代码是这样的:

changes : 变化 :

 ////Added this code : componentWillReceiveProps() { this.fetch(); this.setState({ somekey: 'yes' }); } /// Added Actions.refresh({somekey: 'Yes'}); with some random props activate(uid){ console.log('Lets de-activate all accounts'); var query = "UPDATE users SET active='no' WHERE active=?"; var params = ['yes']; db.transaction((tx) => { tx.executeSql(query,params, (tx, results) => { console.log('all users should be de-active now'); console.log('activating user ', uid); var query = "UPDATE users SET active='yes' WHERE userId=? LIMIT 1;"; var params = [uid]; db.transaction((tx) => { tx.executeSql(query,params, (tx, results) => { console.log('UserId ' , uid , ' is Active now'); this.renderUsers(); Actions.refresh({somekey: 'yes'}); //Actions.bridge({type: 'reset', toGo: 'home'}); }, function(){ console.log('No such a user found to make active'); }); }); }, function(){ console.log('No such a user found to make active'); }); }); } 

Hope it will help others who have the same issue 希望对其他有同样问题的人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM