Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onload in react?

Tags:

reactjs

To run the imagesrore function onload I have to call <img src="image_7.jpg" className="hide" alt="image_7.jpg"/> image but actually there is no use of this line and if I remove this onload doesn't work and function is not called. So how can I call the imagestore() onload in react.

class PicturesList extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
          imagesarray: []
          };
          this.imagestore = this.imagestore.bind(this);
        }
        render() {
          return (
          <div>
            <div onLoad= {() => this.imagestore()}>
                <img src="image_7.jpg" className="hide" alt="image_7.jpg"/>
                // To run the imagesrore function onload I have to call this image but actually there is no use of this line and if I remove this onload doesn't work and function is not called
            </div>
            <Gallery url={this.state.imagesarray}/>
          </div>
          );
        }
        imagestore()
        {
        const imgUrls=this.props.apikeys;
        const objarr = Object.values(imgUrls);
        this.setState({
            imagesarray: objarr
        });
        }

      }

what I want

class PicturesList extends React.Component {
            constructor(props) {
              super(props);
              this.state = {
              imagesarray: []
              };
              this.imagestore = this.imagestore.bind(this);
            }
            render() {
              return (
              <div>
                <div onLoad= {() => this.imagestore()}>

                    // but when I did this imagestore() function not called
                </div>
                <Gallery url={this.state.imagesarray}/>
              </div>
              );
            }
            imagestore()
            {
            const imgUrls=this.props.apikeys;
            const objarr = Object.values(imgUrls);
            this.setState({
                imagesarray: objarr
            });
            }

          }
like image 211
Ayush Srivastava Avatar asked Feb 22 '19 04:02

Ayush Srivastava


People also ask

How do you load data in a react application?

In applications that are data-driven like React, we load data when a view or page is loaded. In react we can create either class or function based components. In a class-based component, we have componentWillMount () or componentDidMount () hooks that can be used to execute any function when the component is going to load or loaded into view.

Do I need to wait for onload in react?

You shouldn't need to wait for onload in react. It is fine to use events to trigger some logic but my experience is that I start rendering right away in the code that I use to include react. Is typescript worth it? I'm thinking of using typescript in a project I'm working on, it's going to be a production application.

What is the use of hooks in React React?

In react we can create either class or function based components. In a class-based component, we have componentWillMount () or componentDidMount () hooks that can be used to execute any function when the component is going to load or loaded into view.

How do you handle events in react JS?

Handling Events. Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences: React events are named using camelCase, rather than lowercase. With JSX you pass a function as the event handler, rather than a string.


2 Answers

Use useEffect() in React. You would use it like this:

useEffect(()=>{

**INSERT CODE YOU WANT RUN ON LOAD HERE **

}, [])

Remember to import useEffect as well with import React, { useEffect } from 'react'

like image 146
Sim Avatar answered Oct 01 '22 10:10

Sim


Instead of rendering the image which you dont want, you could simply load it in componentDidMount like

class PicturesList extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
      imagesarray: []
      };
      this.imagestore = this.imagestore.bind(this);
    }

    componentDidMount() {
       const img = new Image();
        img.onload =() => {
          // image  has been loaded
          this.imagestore()
        };

        img.src = 'image_7.jpg';
    }
    render() {
       return (
          <div>
            </div>
            <Gallery url={this.state.imagesarray}/>
          </div>
       );
    }
    imagestore() {
        const imgUrls=this.props.apikeys;
        const objarr = Object.values(imgUrls);
        this.setState({
            imagesarray: objarr
        });
    }

}

The above solution is just to call imageStore once an image is loaded. However if what you intend is to call imageStore when the component has fully loaded,just trigger this.imageStore() in componentDidMount

class PicturesList extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
      imagesarray: []
      };
      this.imagestore = this.imagestore.bind(this);
    }

    componentDidMount() {
        this.imagestore()
    }
    render() {
       return (
          <div>
            </div>
            <Gallery url={this.state.imagesarray}/>
          </div>
       );
    }
    imagestore() {
        const imgUrls=this.props.apikeys;
        const objarr = Object.values(imgUrls);
        this.setState({
            imagesarray: objarr
        });
    }

}
like image 25
Shubham Khatri Avatar answered Oct 01 '22 09:10

Shubham Khatri