私は何度も何度も何度も何度も何度も何度も何度も何度もやり続けていることに対して、リアクションコンポーネントを拡張しようとしています。これまで私が試してみました:'コンポーネント'を拡張します。 Uncaught TypeError:スーパー式はヌルかオブジェクトではなく関数でなければなりません
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})
}
も、私はこのbindReactMethods
コールバックはオプションであることと、Component
を拡張し、常に私のコンポーネントを使用したい
Uncaught TypeError: Super expression must either be null or a function, not object
に失敗しました。
は、あなたがしているようにも、それは、通常のスーパーすることができますモジュールから 'MyComponent'をエクスポートしますか? – Li357
いいえ私はいなかった、今私は新しいエラーを得る – codyc4321
それは何でしょうか? – Li357