Camper Leaderboard - Is this the wrong way to do it?

I think I need 3 components:

  • LeaderBoard ( Parent)
  • TableHead (Child of LeaderBoard)
  • TableBody (Child of LeaderBoard)

The LeaderBoard-Component:
When the component will mount this component loads the API-datas and give it as a property to the TableBody

The TableBody-Component:
It will display the datas that the LeaderBoars-Componets gives him as a property

The TableHead-Compnent:
No comes the problem
When someone will click to sort by recent or alltime, how can this component update the TableBody-Component?
Or is can childcomponents talk to each other?

One answer could be, that I only have one component. But I think that there has to be 3 Components… or am I wrong?

Here you can finde the codepen.

You can pass functions from Parent as props.

You can have as many components as you like :wink: I did it with one component, but now I’m more inclined to splitting into smaller components.

Here you can see how you can pass function (see console):

1 Like

Yes, you are right! Thanks! :slight_smile:

But know I have a new problem…
I cant store the JSON in an array…

Has someone maybee a solution or a hint?

      var xhr = new XMLHttpRequest();'GET', source, true);
      var data = [];
      xhr.onreadystatechange = processRequest;
      return data;
      function processRequest(e) {
        if (xhr.readyState == 4 && xhr.status == 200) {
          var response = JSON.parse(xhr.responseText);
          return response;

The data array stores somethink but I can’t access it. Like you can see in the console…



get executed before the response is received, that’s why you got empty arrays.

Add console.log(data[0][1]); after data.push(response); in processRequest(e)


        recent: this._loadData(''),
        alltime: this._loadData('')

recent is ‘‘empty’’ too. It stores somethink but I can’t access it.

It looks like you are currently editing your code, because it keeps changing all the time, but general idea is that you should either use this.setState() in processRequest(e) or use Promises.

It doesn’t store anything, hover over blue i symbol in console and you’ll see what it stores.

thanks @jenovs! There is a fetch API that uses Promises and now it works! :slight_smile:
just for fatching the data I needed 3 houres… haha

 _setState(stateKey, source){
        .then(function(response) {
        return response.json();
        .then(function(json) {
        var stateObj = {};
        stateObj[stateKey] = json;
        .catch(function(error) {
        console.error('Fetch:' + error);

I finished it! :smiley:
just have to style it :slight_smile:

Here you can see it on codepen

@jenovs thanks for your help