【发布时间】:2019-01-19 03:59:01
【问题描述】:
在下面提供的代码中,我可以看到 this.mBaseService 定义在第一个 debugger 之上,但不在第二个 debugger 之下。
有人能解释为什么会这样吗?我的一个理论是 React 或 Next.js 可能在内部清除属性。
import Link from 'next/link';
import React, { Component, Fragment } from 'react';
import ReactDropzone from 'react-dropzone';
import { Container, Row, Col, Button, Jumbotron, ListGroup, ListGroupItem } from 'reactstrap';
import DefaultHomeView from '../components/default-home-view';
import Page from '../components/page';
import EteeReport from '../components/etee-report';
import Layout from '../components/layout';
import { ServiceBaseService } from '../services/service-base/service-base.service';
export default class extends Page {
mBaseService = new ServiceBaseService();
constructor(props) {
super(props);
this.state = {
files: [],
};
console.log('it exists!', this.mBaseService);
debugger;
//this.mBaseService = new ServiceBaseService();
}
fHandleOnDrop = async files => {
debugger;
const oResponse = await this.mBaseService.fpPost('/reports', {
oDataToPost: JSON.stringify(files),
});
// TODO: if valid csv then parse and chart the data
this.setState({
files: this.state.files.concat(files),
});
};
fClearFiles = () => {
this.setState({
files: [],
});
};
render() {
return (
<Layout {...this.props} navmenu={false} container={false}>
{!this.state.files.length && (
<DefaultHomeView {...this.props} fHandleOnDrop={this.fHandleOnDrop} files={this.state.files} />
)}
{this.state.files.length && <EteeReport {...this.props} {...this.state} fClearFiles={this.fClearFiles} />}
</Layout>
);
}
}
【问题讨论】:
-
如果你在构造函数或
fHandleOnDrop中console.log(this.mBaseService)我想你会看到它在两个地方都可以访问。我不确定为什么你在调试器中看不到它。 -
确实如此。诡异的。如果我在
fHandleOnDrop中调用console.log(this.mBaseService),我会看到预期的结果,即使我在开发工具中并且我将this.mBaseService登录到控制台,它也是未定义的。我仍然想知道为什么,但如果有人推荐,我会关闭。 -
@Tholle 这回答了我也有的第二个问题。你看这个
class extends Page怎么样?好吧,Page也有mBaseService = new ServiceBaseService();,但是在调试时它没有显示给我。现在,如果我在子类上评论该属性并且我console.log我可以看到它,即使我debugger并在控制台上询问它是未定义的。
标签: javascript reactjs ecmascript-6 es6-class next.js