react-native - Event 组件不断调用 componentDidMount 但根本不加载
问题描述
我的react native 0.59
应用中有 3 个底部选项卡,带有react navigation 3.7
. 选项卡是Event
和。是初始选项卡。Group
Contact
Event
初始加载后,我单击Group
选项卡,屏幕如下:
显示了 2 组 HT 和 MKL。在我单击一个组(HT 或 MKL)后,这应该会加载Event
组件。问题是单击组后,Event
永远不会加载。在应用程序和服务器监控屏幕上,都有疯狂的加载Event
和Group
不停。屏幕卡住Group
并停止响应任何点击。我对这种行为一无所知,因为Event
模块已经过全面测试并且Group
是新添加的。
这是Group
模块中的 onPress 代码:
async _onPress(id) {
try {
console.log("saved group id in Group : ", id);
await helper.setMyValue("group_id", id.toString());
//update group_id in App.js
this.props.updateGroup(id); //<<<===this causes the problem
} catch (err) {
console.log("error in saving group_id to local disk", err);
};
this.props.navigation.goBack();
//this.props.navigation.navigate("Event"); //, {group_id:element1.id});
}
上面的 onPress 所做的是保存id
用户单击并调用updateGroup
以更新父级的 group_id 并调用goBack
哪个Event
组件。
componentDidMount
里面是Event
:
async componentDidMount(){
this._isMounted = true;
if (!this.props.group_id) {
alert("Select a group");
return;
};
try {
//retrieving token
let result = this.state.token;
if (!result) {
result = await helper.getJwtToken();
if (this._isMounted) this.setState({
token: result
});
};
//console.log("secure store : ", result);
if (!result) {
this.props.navigation.navigate("Signup");
return;
};
this.refreshData(result);
//refresh data after goBack()
this.willFocusSubscription = this.props.navigation.addListener(
'willFocus', () => {
this.refreshData(result);
}
);
//get user
let myself = this.state.user;
if (!myself) {
myself = await helper._getUser(result.password, result.username);
if (this._isMounted) this.setState({
user: myself
});
};
console.log("out of did mount event", this.state);
}
catch (err) {
console.log("服务器错误(et),请再试一次!", err);
this.props.navigation.navigate.goBack(); //("Event");
return;
};
这是在Event
componentDidMount 中的应用程序控制台输出:
09-19 20:16:44.551 21659 21743 I ReactNativeJS: 'event url : ', 'http://192.168.2.133:3000/api/events/active?group_id=1&_group_id=1&_device_id=0cfce7b7e86fa397&_timezone=America%2FLos_Angeles'
09-19 20:16:44.552 21659 21743 I ReactNativeJS: 'out of did mount event', { activeEvents: [],
}
更新:
问题是由this.updateGroup(id)
哪个更新了group_id的状态引起的App.js
updateGroup = (group_id) => {
if (this._isMounted) this.setState({group_id:group_id});
};
它Group
作为道具传递给App.js
:
const GroupWithSelf = (props) => (<Group {...props} myself={data.myself} token={data.result} updateGroup={this.updateGroup} updateToken={this.updateToken} />);
不知道为什么这种方法会导致赛车。
解决方案
推荐阅读
- javascript - DeviceMotionEvent 的 IOS safari 13.1 权限
- angular - 创建一个指令来监视角度生命周期钩子
- batch-file - .vbs 脚本在批处理文件中运行,但在通过任务调度程序运行时不运行
- c# - 与 Xamarin.Forms Android 项目建立 WebSocket4Net 连接时遇到问题
- sql - 这个错误是什么意思以及如何解决它
- java - 如何生成 LogN 唯一数字的排序列表,其中 N 是数组的给定大小?
- angular - 调整 MatRadioButton 的大小 - 涟漪问题
- javascript - 如何在 ThreeJS 中单击鼠标的位置准确地绘制几何形状
- windows - 为什么 couchdb 和 fauxton 在 Windows 10 中无法正常工作?
- exception - Spring Integration - 自定义 errorChannel - 仅记录第一个异常