Image onLoad event in isomorphic/universal react - register event after image is loaded

后端 未结 5 748

In isomorphic rendered page image can be downloaded before main script.js file. So image can be already loaded before react register onLoad

5条回答
  •  Happy的楠姐
    2020-12-23 15:12

    Another way is to use ref and cover those both scenarios:

     {
        // onLoad replacement for SSR
        if (!input) { return; }
        const img = input;
    
        const updateFunc = () => {
          this.setState({ loaded: true });
        };
        img.onload = updateFunc;
        if (img.complete) {
          updateFunc();
        }
      }}
      src={imgSrc}
      alt={imgAlt}
    />
    

提交回复
热议问题