使用stopPropagation时,无法读取未定义的属性'findDOMNode' - reactJs

时间:2017-12-13 02:55:56

标签: javascript reactjs event-bubbling react-dom

我需要通过深层嵌套组件进行事件冒泡,我试着做一些像下面这样的事情

   import {ReactDOM,render, unmountComponentAtNode} from 'react-dom';

  .........
  .........

  componentDidMount() {
    ReactDOM.findDOMNode(this).addEventListener('click', (event) => {
    event.stopPropagation();
  }, false)
}

我收到以下错误

bundle.js:45 Uncaught TypeError: Cannot read property 'findDOMNode' of undefined

1 个答案:

答案 0 :(得分:0)

<div class="not_fixed"> this is a pretty long title </div> <BR> <div class="fixed"> <span>this is a pretty long title</span> </div>是默认导出,导入ReactDOM而不是findDOMNode。 试试这个:

ReactDOM

OR

import {findDOMNode ,render, unmountComponentAtNode} from 'react-dom';
...
componentDidMount() {
    findDOMNode(this).addEventListener('click', (event) => {
    event.stopPropagation();
  }, false)
}