繁体   English   中英

React this.props.“function”不是function

[英]React this.props.“function” is not a function

我花了很多时间在堆栈溢出上,尽管我进行了研究,但我仍然找不到我的错误。 我想使用一个名为“mxGraph”的外部库中的方法,并且我想添加一个我的图形组件的侦听器

这是我的错误:“this.props.graph.getSelectionModel 不是函数”

这是我的代码:

class StyleModificationBar extends Component {
    constructor(props) {
        super(props);
        this.state = {
            fontValue: 16,
        };
    }

    componentDidMount() {
        this.initListener();
    }

    initListener = () => {
        this.props.graph.getSelectionModel().addListener(mxEvent.CHANGE, this.onSelected);
    };

    onSelected = evt => {
        console.log(evt.cells[0]);
    };
}

我已经尝试了所有方法,在构造函数中绑定了我的 initLister function,我一次又一次地尝试在构造函数中绑定我的 initLister function,我一次又一次地尝试。

有人可以帮助我吗?

The behavior is very strange, the first console.log (the one in the constructor) returns an empty object while the one in the increase function returns my mxGraph object and this function works perfectly.

class StyleModificationBar extends Component {
        constructor(props) {
            super(props);
            this.state = {
                fontValue: 16,
            };
            console.log(this.props.graph); // This one print a empty object
        }

        increaseFontValue = () => {
            console.log(this.props.graph); // This one print a full mxGraph object
            let cellProperties = this.props.graph.getCellStyle(this.props.graph.getSelectionCell());
            this.setState({fontValue: parseInt(cellProperties.fontSize) + 1},
                () => {
                    this.props.graph.setCellStyles("fontSize", this.state.fontValue)
                })
        }

        decreaseFontValue = () => {
            let cellProperties = this.props.graph.getCellStyle(this.props.graph.getSelectionCell());
            this.setState({fontValue: parseInt(cellProperties.fontSize) - 1},
                () => {
                    this.props.graph.setCellStyles("fontSize", this.state.fontValue)
                })
        }

暂无
暂无

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

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