【发布时间】:2019-09-04 10:43:11
【问题描述】:
在我的 ReactJS@15 项目中,我遇到了 unmount setState change warning 的问题,但组件已安装。
这是应用程序结构:
/app
/container
/veil
/router
/routed-view
/other-routed-view
我还有一个“veil manager class”,它以这种方式触发附加到veil组件的“_toggle”事件:
componentDidMount()
{
VeilManager.i().on('toggle', this._toggle.bind(this));
}
_toggle(payload = {})
{
const { veil = false, cb } = payload;
this.setState({ isOpen: veil }, cb);
}
从路由视图我触发以下代码:
componentDidMount()
{
VeilManager.i().toggle(this._otherFunc.bind(this));
}
在调试流程中,Veil 组件在触发事件时被标记为已卸载,但完全没有意义,因为容器已经注册到其子级。
更重要的是,Veil 组件会按预期做出反应,因此当 VeilManager 状态更改时,Veil 会切换进出。
有什么建议吗?
扩展代码:
// Veil.js
import { background, logo, veil } from './Styles';
import React, { Component } from 'react';
import VeilManager from './Manager';
export default class Veil extends Component
{
constructor(props)
{
super(props);
this.state = {
isOpen: false
};
}
componentDidMount()
{
VeilManager.i().on('toggle', this._toggle.bind(this));
}
_toggle(payload = {})
{
const { veil = false, cb } = payload;
this.setState({ isOpen: veil }, cb);
}
/**
* @override
*/
render()
{
const { isOpen = false } = this.state;
return (
<div style={veil(isOpen)} className="veil-wrapper">
<div style={logo} className="veil-wrapper__logo"/>
<div style={background}/>
</div>
);
}
}
// VeilManager.js
const { EventEmitter } = require('events');
/**
* VeilManager singleton reference
* @type {null}
*/
let $iManager = null;
/**
* Handles the status of veil
* @class veil.Manager
* @extends EventEmitter
*/
export default class Manager extends EventEmitter
{
/**
* @constructor
*/
constructor()
{
super();
this.isOpen = false;
}
/**
* Toggles "isOpen" status
* @param {null|Function} cb To execute after toggling
*/
toggle(cb = null)
{
this.isOpen = !this.isOpen;
this.emit('toggle', { veil: this.isOpen, cb });
}
/**
* Returns the singleton instance of VeilManager
* @return {null|Manager} Singleton instance
*/
static i()
{
$iManager = $iManager || new Manager();
return $iManager;
}
}
//Container.js
import 'react-s-alert/dist/s-alert-css-effects/slide.css';
import 'react-s-alert/dist/s-alert-default.css';
import { Redirect, Route, Switch } from 'react-router-dom';
import React, { Component } from 'react';
import Alert from 'react-s-alert';
import Aside from '@/components/Aside';
import Header from '@/components/Header';
import token from '@/orm/Token';
import Sidebar from '@/components/Sidebar';
import Veil from '@/components/Veil';
// VIEWS
import Clients from '../../views/Clients/';
import Dashboard from '@/views/Dashboard';
export default class Full extends Component
{
/**
* @override
*/
render()
{
return (
<div className="app">
<Header />
<div className="app-body">
<Sidebar {...this.props}/>
<main className="main">
<Veil/>
<div className="container-fluid">
{ token.hasExpired()
? <Redirect to="/login"/>
: <Switch>
<Route path="/dashboard" name="Dashboard" component={Dashboard}/>
<Route path="/clients" name="Clients" component={Clients}/>
<Redirect to="/dashboard"/>
</Switch>
}
</div>
</main>
<Aside />
</div>
<Alert stack={{ limit : 3 }} />
</div>
);
}
}
//Clients.js
import React, { Component } from 'react';
import onFetch from '@/mixins/on-fetch';
/**
* Clients view.
*/
export default class ClientsIndex extends Component
{
/**
* @constructor
*/
constructor(props)
{
super(props);
this._onFetch = onFetch;
}
/**
* @override
*/
componentDidMount()
{
this._onFetch();
}
/**
* @override
*/
render()
{
return (
<div className="animated fadeIn">
<div className="row">
...
</div>
</div>
);
}
}
//mixin/on-fetch
import VeilManager from '@/components/Veil/Manager';
export default (cb = null) => {
debugger;
VeilManager.i().toggle(cb);
};
【问题讨论】:
-
更新可能会冒泡并导致高位组件刷新,这意味着 setstate 现在作为之前未安装的组件无效
-
这看起来像是冒泡的情况,你能提供更多代码吗?
-
但是 Veil 组件是独立的,容器组件不与其子组件或 Veil 本身共享其状态。孩子们也不会向上分享状态。
-
@DarpanRangari 添加了额外的代码
-
@ArturoMartínezDíaz 未声明,冒泡。 DOM 中的事件从子级向上冒泡到父级,React 不会改变这一点。如果您使用
CustomEvent构造函数,也可以将其设置为 false。另外,你是如何在浏览器中使用 EventEmitter 的?
标签: javascript reactjs lifecycle