如何删除事件侦听器,以便在卸载组件时它不会尝试更新状态?
How can I remove event Listener so it would not try to update state when component is unmounted?
我设置了一些动态样式,一切正常,除非卸载组件。然后它抛出错误:无法在未安装的组件上调用 setState(或 forceUpdate)。
这是堆栈导航器中的第二个屏幕,当我转到第三个屏幕时,一切正常,但是当我转到第一个屏幕并卸载组件时,它会引发错误。
我试图删除 componentWillUnmount 中的事件侦听器但没有成功,显然我做错了什么。
此外,我已经尝试过这种方法 this.props.navigation.isFocused() 并且它再次工作得很好,但是如果我在第三个屏幕上并旋转设备然后返回,Dimensions 事件监听器看不到更改和样式一团糟。
那么如何在卸载组件时停止事件侦听器?
提前致谢。
constructor(props) {
super(props);
Dimensions.addEventListener("change", dims => {
// this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get("window").height > 500 ? "column" : "row",
imageHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoWidth: Dimensions.get("window").height > 500 ? "100%" : "50%"
}
});
// : null;
});
}
componentWillUnmount
componentWillUnmount() {
console.log("Unmounted");
Dimensions.removeEventListener("change", dims => {
// this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get("window").height > 500 ? "column" : "row",
imageHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoWidth: Dimensions.get("window").height > 500 ? "100%" : "50%"
}
});
// : null;
});
}
你应该像这样创建一个命名函数(准确地说是方法)
fun_name(){
// this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get("window").height > 500 ? "column" : "row",
imageHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoWidth: Dimensions.get("window").height > 500 ? "100%" : "50%"
}
});
// : null;
}
constructor(props) {
super(props);
Dimensions.addEventListener("change", this.fun_name);
}
componentWillUnmount() {
console.log("Unmounted");
Dimensions.removeEventListener("change", this.fun_name);
}
PS: 别忘了绑定函数
单独定义一个函数,add/remove 该特定函数的监听器。
在安装组件后还要添加侦听器,以避免在未安装的组件上设置状态
componentDidMount () {
Dimensions.addEventListener('change', updateDimensions)
}
componentWillUnmount() {
Dimensions.removeEventListener('change', updateDimensions)
}
updateDimensions() {
this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get('window').height > 500 ? 'column' : 'row',
imageHeight: Dimensions.get('window').height > 500 ? '50%' : '100%',
infoHeight: Dimensions.get('window').height > 500 ? '50%" : '100%',
infoWidth: Dimensions.get('window').height > 500 ? '100%' : '50%'
}
});
: null;
}
我设置了一些动态样式,一切正常,除非卸载组件。然后它抛出错误:无法在未安装的组件上调用 setState(或 forceUpdate)。
这是堆栈导航器中的第二个屏幕,当我转到第三个屏幕时,一切正常,但是当我转到第一个屏幕并卸载组件时,它会引发错误。
我试图删除 componentWillUnmount 中的事件侦听器但没有成功,显然我做错了什么。
此外,我已经尝试过这种方法 this.props.navigation.isFocused() 并且它再次工作得很好,但是如果我在第三个屏幕上并旋转设备然后返回,Dimensions 事件监听器看不到更改和样式一团糟。
那么如何在卸载组件时停止事件侦听器?
提前致谢。
constructor(props) {
super(props);
Dimensions.addEventListener("change", dims => {
// this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get("window").height > 500 ? "column" : "row",
imageHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoWidth: Dimensions.get("window").height > 500 ? "100%" : "50%"
}
});
// : null;
});
}
componentWillUnmount
componentWillUnmount() {
console.log("Unmounted");
Dimensions.removeEventListener("change", dims => {
// this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get("window").height > 500 ? "column" : "row",
imageHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoWidth: Dimensions.get("window").height > 500 ? "100%" : "50%"
}
});
// : null;
});
}
你应该像这样创建一个命名函数(准确地说是方法)
fun_name(){
// this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get("window").height > 500 ? "column" : "row",
imageHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoHeight: Dimensions.get("window").height > 500 ? "50%" : "100%",
infoWidth: Dimensions.get("window").height > 500 ? "100%" : "50%"
}
});
// : null;
}
constructor(props) {
super(props);
Dimensions.addEventListener("change", this.fun_name);
}
componentWillUnmount() {
console.log("Unmounted");
Dimensions.removeEventListener("change", this.fun_name);
}
PS: 别忘了绑定函数
单独定义一个函数,add/remove 该特定函数的监听器。 在安装组件后还要添加侦听器,以避免在未安装的组件上设置状态
componentDidMount () {
Dimensions.addEventListener('change', updateDimensions)
}
componentWillUnmount() {
Dimensions.removeEventListener('change', updateDimensions)
}
updateDimensions() {
this.props.navigation.isFocused() ?
this.setState({
resStyles: {
imageFlex: Dimensions.get('window').height > 500 ? 'column' : 'row',
imageHeight: Dimensions.get('window').height > 500 ? '50%' : '100%',
infoHeight: Dimensions.get('window').height > 500 ? '50%" : '100%',
infoWidth: Dimensions.get('window').height > 500 ? '100%' : '50%'
}
});
: null;
}