React.createClass内部的作用域问题

Scope issue inside React.createClass

本文关键字:作用域 问题 内部 createClass React      更新时间:2023-09-26

我有一个范围问题,我正在努力解决。

我将getTrackNotes导入到es6模块中,我想在React类中引用它,但这被设置为Track (React类)。

我如何引用它?

import { getTrackNotes } from '../utils/immutableInstruments';
const Track = React.createClass({
    componentDidMount() {
        const { trackId } = this.props.params;
        function updateTimeSequencer (socket, state, getTrackNotes, TimeSequencer, instruments, trackId)
        {
            //I want to call getTrackNotes here, but the scope is set to the Track React class
            **getTrackNotes();**
        }
        var instruments = this.props.instruments;
        socket.on('state', updateTimeSequencer.bind(this, socket));
     }

您的getTrackNotes函数将从顶部作用域继承,但您使用同名函数参数重写它。只要从函数参数列表中删除getTrackNotes或将参数重命名为其他内容:

function updateTimeSequencer (socket, state, thisIsSomethingElse, TimeSequencer, instruments, trackId)