0

I'm trying to get a list of posts in a SelectControl by building an object with the folowing function declared inside edit

edit: function (props) {      
var getPosts = function(){
        let posts = [];
        $.getJSON('/wp-json/wp/v2/pages?per_page=100', function(data) {
            $.each(data, function(index, elem){
                posts.push({
                    label: data[index]['title']['rendered'],
                    value: data[index]['link']
                })
            })
        });
        console.log(posts);
        return props.setAttributes({
          selectPages: posts,
        })
    }

return[...]
    }

My question is where do I call the function getPosts() ? because calling it right before return of the edit function goes into an infinite loop until react crashes.

My goal is to call this function before SelectControl so the Select list will get populated automatically when the block it's added on page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.