オプション
あなたはES6使用している場合、これは非常に簡単です:
は、別のJavaScriptファイル内のutilの関数を定義します。 '../path/to/utils/updateDimension':その後、
export function updateDimension(Component) {
this.setState({
height: window.innerHeight,
});
}
そして、このようなあなたのコンポーネントでそれを使用する:@updateDimensionは種類のクラスを拡張するYourComponentを言っている
import { updateDimension } from '../path/to/utils/updateDimension';
@updateDimension export class YourCompoennt extends React.Component {
// ...
}
そのupdateDimension関数によって呼び出されます。
オプションB
もう一つの方法は、高次のコンポーネントを利用するには、次のようになります。あなたのコンポーネントのコールのそれぞれの下部に続いて
import React from 'react';
export function updateDimension(WrappedComponent) {
return class Dimensions extends React.Component {
updateDimension() {
this.setState({
height: window.innerHeight,
});
}
render() {
return <WrappedComponent {...this.props}/>
}
}
}
:
は、このようなあなたのutilの定義:
export default updateDimension(YourComponent)();
ありがとうございます。オプションBでウィンドウリサイズリスナを追加するにはどうすればよいですか? – vuvu