1
私の理解から、ReactJSのHOは装飾されたコンポーネントに小道具を追加します。しかし、私はstate
でも動作できるメソッドを追加したいと思います。一例として、私は一般的にthis.isMounted()
をチェックせずにthis.setState
を呼ぶことはありません。本質的には、私は欲しい:Reactの上位コンポーネントにメソッドを追加
export default ComposedComponent => class BaseComponent extends React.Component {
static displayName = "BaseComponent";
constructor(props) {
super(props);
}
//------> I want this method to be available to any ComposedComponent
//------> And it has to act upon the state of ComposedComponent
updateState(obj) {
if (this.isMounted() && obj) {
this.setState(obj);
}
}
render() {
return (
<ComposedComponent {...this.props} {...this.state} />
)
}
}
私のコンポーネントを飾るしたいと思うHome
。だから私はexport default BaseComponent(Home)
と返すだけです。
this.updateState
はHome
クラスでは使用できません。これをどうやって解決するのですか?