永远不会触发onLoad事件

时间:2017-05-17 00:12:29

标签: javascript reactjs event-handling

我试图弄明白为什么onload事件永远不会被调用。所有内容都构建没有错误,类似的onLoad事件在不同的组件中工作。     export class Work扩展了React.Component {

  constructor(props) {
    super(props);


    this.state = {
      allImgLoaded: false,
      boxHovered: false,
      projects: projects
    }
this.handleWorkLoad = this.handleWorkLoad.bind(this);

  }

componentWillMount() {
  window.scrollTo(0,0);
}

handleWorkLoad() {
  console.log("called");
  this.setState({
    allImgLoaded: true
  })
}

  render() {

    return (
    <div className={this.state.allImgLoaded ? "work" : "work invisible"} onLoad={this.handleWorkLoad}>

        </div>    
    );
  }
};

export default Work;

1 个答案:

答案 0 :(得分:1)

在您的反应中,您有componentDidMount方法,该方法使您能够在组件完成加载阶段后运行特定代码:

class Work extends React.Component {
    constructor(props) {
        super(props);


        this.state = {
          allImgLoaded: false,
          boxHovered: false,
          projects: projects
        }
        this.handleWorkLoad = this.handleWorkLoad.bind(this);

    }

    componentWillMount() {
        window.scrollTo(0,0);
    }

    componentDidMount() {
        this.handleWorkLoad()
    }

    handleWorkLoad() {
        console.log("called");
        this.setState({
            allImgLoaded: true
        })
    }

    render() {

        return (
            <div className={this.state.allImgLoaded ? "work" : "work invisible"}>
            </div>    
        );
    }
};

export default Work;

反应中onLoad个元素没有div个事件。