Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJS Stores-this.context.getStore不是函数(…;)_Javascript_Reactjs - Fatal编程技术网

Javascript ReactJS Stores-this.context.getStore不是函数(…;)

Javascript ReactJS Stores-this.context.getStore不是函数(…;),javascript,reactjs,Javascript,Reactjs,我有: 但我把它放在最上面: this.context.getStore is not a function 我哪里做错了 这是我的商店: static contextTypes = { getStore: React.PropTypes.func.isRequired }; 您必须更改构造函数,因为您通过构造函数方法参数接收道具和上下文: import { EventEmitter } from 'events'; class JobStore extends

我有:

但我把它放在最上面:

this.context.getStore is not a function
我哪里做错了


这是我的商店:

static contextTypes = {
        getStore: React.PropTypes.func.isRequired
    };

您必须更改构造函数,因为您通过构造函数方法参数接收道具和上下文:

import { EventEmitter } from 'events';

class JobStore extends EventEmitter {

    constructor() {
        super();
        this.jobs = new Map();
        this.counts = {};
    }

    handleJobsData(payload) {
        console.log(payload)
        if (payload.clear === true) {
            this.jobs = new Map();
        }
        payload.data.jobs.forEach((job) => {
            this.jobs.set(job.id, job);
        });
        this.counts = payload.data.counts;

        this.emit('change');

    }

    getCounts() {
        return this.counts;
    }

    getJobs() {
        return this.jobs;
    }

    dehydrate () {
        return this.jobs;
    }

    rehydrate (state) {

    }

}

JobStore.dispatchToken = null;

JobStore.handlers = {
    'RECEIVED_JOBS_DATA': 'handleJobsData'
};

JobStore.storeName = 'JobStore';

export default JobStore;

如果不这样做,您的属性和上下文对象将丢失

我不知道为什么,但删除后:

constructor(props, context) {
    super(props, context);
}
它起作用了

并将顶部更改为:

RightPanel.contextTypes = {
    executeAction: React.PropTypes.func.isRequired
};

export default RightPanel;

下一个问题是覆盖上下文类型。RightPanel.contextTypes和静态声明相等。这就是在类中移动ContextTypes后得到正确结果的原因。
RightPanel.contextTypes = {
    executeAction: React.PropTypes.func.isRequired
};

export default RightPanel;
static contextTypes = {
        getStore: React.PropTypes.func.isRequired,
        executeAction: React.PropTypes.func.isRequired
    };