私はmousemoveイベントを捕捉したい反応容器を持っています。それはかなり簡単です。さらに、実際にmousemoveイベントハンドラのロジックが存在するコンテナによってレンダリングされた反応サブコンポーネントがあります。だから私はコンテナの親から子にmousemoveイベントを渡す/委譲し、子のmousemoveハンドラを呼び出す必要があります。それはどうやって正しく行われますか?ReactJS - 子イベントハンドラで親イベントを処理する
コンテナがDOMのより大きなHTML要素であり、その全体領域内のイベントをキャプチャする必要があるため、サブコンポーネントではなくコンテナでマウスイベントをキャプチャする必要があります。
mousemove-handler-logicは、コンテナが認識してはならない機能をカプセル化するため、サブコンポーネント内に存在する必要があります。
コンテナのレンダリング方法:
render() {
return (
<div data-component="MyContainer"
onMouseMove={(event) => this.handleMousemove(event)}>
<MySubComponent {// ... give some props here}>
</div>
);
}
に私はMyContainerは、そのDOMノードを取得し、addEventListenerをしてハンドラを登録するMySubComponentのためのコールバック関数を設定しますが、それは一貫として時折DOMノードを動作しませんでしたアプローチを試してみました未定義ました:
export default class MyContainer extends Component {
constructor(props) {
super(props);
this.state = {};
this.getContainerRef = this.getContainerRef.bind(this);
}
getContainerRef() {
return this.refs['containerWrap'];
}
render() {
return (
<div data-component="MyContainer"
ref="containerWrap"
onMouseMove={(event) => this.handleMousemove(event)}>
<MySubComponent getContainerRef={this.getContainerRef} />
</div>
);
}
}
export default class MySubComponent extends Component {
constructor(props) {
// ... init something
this.handleMousemove = this.handleMousemove.bind(this);
}
componentDidMount() {
// add event listener for parent application frame
let containerDOM = this.props.getContainerRef();
containerDOM.addEventListener('mousemove', this.handleMousemove, false);
}
componentWillUnmount() {
// remove event listener from parent application frame
let containerDOM= this.props.getContainerRef();
containerDOM.removeEventListener('mousemove', this.handleMousemove, false);
}
handleMousemove(event) {
// handle the event
}
// ... more methods
}
は、私はまた、this.refs.SubComponent.handleMousemove
経由MyContainerから直接MySubComponentでのMouseMoveイベントハンドラを呼び出すことを試みたが、それが反応し、Reduxの中に悪い習慣とみなしています。
あなたの質問に「還元」はありません。あなたは 'onMouseMove'イベントを' MySubComponent'によって直接処理できますか?ロジックが「MySubComponent」によって完全に処理されているのがより理にかなっています – xiaofan2406
最初のアプローチのコードを教えてもらえますか? –
@ xiaofan2406:質問の編集を参照してください。非常に小さなサブコンポーネントではなく、大きなHTMLコンテナ要素でイベントをキャプチャする必要があります。 – Michbeckable