javascript - 扩展 'Component';未捕获的 TypeError : Super expression must either be null or a function, 不是对象

标签 javascript reactjs ecmascript-6

我正在尝试扩展 React 组件来完成我不喜欢一遍又一遍地做的事情,this.onInputChange = this.onInputChange.bind(this);要求。到目前为止我已经尝试过:

MyComponent.js-

import React, { Component } from 'react';


// function bindReactMethod(this_value, method_name) {
//     // Bind all of your custom methods, like:
//     //   this.onInputChange = this.onInputChange.bind(this);
//     this_value[method_name] = this_value[method_name].bind(this_value)
// }


// https://stackoverflow.com/questions/15192722/javascript-extending-class
class MyComponent extends Component {

    constructor(props) {
        super(props);
        this.bindReactMethods = this.bindReactMethods.bind(this)
    }

    bindReactMethods(this_value, method_name) {
        // Bind all of your custom methods, like:
        //   this.onInputChange = this.onInputChange.bind(this);
        this_value[method_name] = this_value[method_name].bind(this_value)
    }
}

SearchBar.js-

import React from 'react';
import MyComponent from '../utils/MyComponent';


export default class SearchBar extends MyComponent {

    constructor(props) {
        super(props);
        this.state = {term: ''};
        this.bindReactMethods(['onInputChange'])
    }

    onInputChange(event) {
        console.log(event.target.value);
        this.setState({term: event.target.value})
    }

失败并显示

Uncaught TypeError: Super expression must either be null or a function, not object

MyComponent.js-

import React, { Component } from 'react';


function bindReactMethod(this_value, method_name) {
    // Bind all of your custom methods, like:
    //   this.onInputChange = this.onInputChange.bind(this);
    this_value[method_name] = this_value[method_name].bind(this_value)
}


// https://stackoverflow.com/questions/15192722/javascript-extending-class
class MyComponent extends Component {

    constructor(props, custom_methods=[]) {
        super(props);
        try {
            custom_methods.map((method_name) => {
                bindReactMethod(this, method_name)
            });
        }
        catch (error) { } // ignore error because that means the user didnt have custom methods to bind
    }
}

SearchBar.js-

import React from 'react';
import MyComponent from '../utils/MyComponent';


export default class SearchBar extends MyComponent {

    constructor(props) {
        super(props, ['onInputChange']);
        this.state = {term: ''};
    }

    onInputChange(event) {
        console.log(event.target.value);
        this.setState({term: event.target.value})
    }

也失败了

Uncaught TypeError: Super expression must either be null or a function, not object

我想延长Component并始终使用我的组件,即 bindReactMethods回调是可选的。

最佳答案

MyComponent 只是没有被导出,代码的工作原理如下:

import React, { Component } from 'react';


function bindReactMethod(this_value, method_name) {
    // Bind all of your custom methods, like:
    //   this.onInputChange = this.onInputChange.bind(this);
    this_value[method_name] = this_value[method_name].bind(this_value)
}


// https://stackoverflow.com/questions/15192722/javascript-extending-class
export default class MyComponent extends Component {

    constructor(props, custom_methods=[]) {
        super(props);
        try {
            custom_methods.map((method_name) => {
                bindReactMethod(this, method_name)
            });
        }
        catch (error) { } // ignore error because that means the user didnt have custom methods to bind
    }
}

import React from 'react';
import MyComponent from '../utils/MyComponent';


export default class SearchBar extends MyComponent {

    constructor(props) {
        super(props, ['onInputChange']);
        this.state = {term: ''};
    }

    onInputChange(event) {
        console.log(event.target.value);
        this.setState({term: event.target.value})
    }

它还允许常规 super ,例如

constructor(props) {
            super(props);

关于javascript - 扩展 'Component';未捕获的 TypeError : Super expression must either be null or a function, 不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357129/

相关文章:

javascript传递给函数的包含空格的字符串在第一个空格处被 chop ?

javascript - 转义不存在的元素的常用方法是什么(就像 jQuery 那样)

javascript - Joi 验证显示自定义错误消息

javascript - 当子组件调用方法时,父引用为 null - ReactJS

javascript - 使用 CSS 和 Javascript 淡入淡出文本

javascript - 当在浏览器中直接访问 URL 但单击到时, Angular 路由不起作用?

reactjs - 从 Ant Design Table 获取当前表数据

reactjs - 使用 React Router 传递状态最简单的方法是什么?

javascript - 选择 Array 中给定索引任一侧最近的 2 个元素

javascript - 如何在javascript中使模块异步