React Js componentDidMount Ajax Call setstate does not update state











up vote
0
down vote

favorite












import React, { Component} from 'react';
import { Route } from 'react-router';
import axios from 'axios';

class App extends Component {
constructor(props) {
super(props);
this.state = {
league: {
teams: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
players: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
games: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
error: false
}
};
}

componentDidMount() {
this.getTeamsHandler();
}

getTeamsHandler = () => {
axios.get('/api/League/GetTeams')
.then((response) => {
let prevState = [...this.state.league.teams];
prevState.data = response.data;
prevState.loaded = true;
this.setState({ teams: prevState });
})
.catch((error) => {
this.setState({ error: error });
});
}

renderTeamsHandler = () => {
let games = this.state.league.games;
let content = null;
if (games.data.length > 0) {
content = games.data.map((team, index) => {
return <div key={index}>{team.teamName}</div>;
});
}
return content;
}

render() {
let Team = this.renderTeamsHandler();
return (
<div>
{Team}
</div>
);
}
}

export default App;


The Ajax call does set data to prevState.Data but by the time it gets to rendering it, the state is the same as before the Ajax call. It is very confused as this all looks correct to me. Is it potentially async issue? If that is the case, why previously what I've done calls like this and had no issue at all.



Thanks for any help in advance.










share|improve this question
























  • I think it's because you are setting value on state.teams instead of state.league.teams.
    – klvenky
    Nov 22 at 8:20












  • Add a break point inside your success and step into your code. Once you've hit your break point step inside the function call and see whats going on.
    – Luke Walker
    Nov 22 at 8:23










  • "let prevState = [...this.state.league.teams];". Shouldn't it be "let prevState = {...this.state.league.teams};" (notice curly braces)
    – Anton Pastukhov
    Nov 22 at 9:21















up vote
0
down vote

favorite












import React, { Component} from 'react';
import { Route } from 'react-router';
import axios from 'axios';

class App extends Component {
constructor(props) {
super(props);
this.state = {
league: {
teams: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
players: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
games: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
error: false
}
};
}

componentDidMount() {
this.getTeamsHandler();
}

getTeamsHandler = () => {
axios.get('/api/League/GetTeams')
.then((response) => {
let prevState = [...this.state.league.teams];
prevState.data = response.data;
prevState.loaded = true;
this.setState({ teams: prevState });
})
.catch((error) => {
this.setState({ error: error });
});
}

renderTeamsHandler = () => {
let games = this.state.league.games;
let content = null;
if (games.data.length > 0) {
content = games.data.map((team, index) => {
return <div key={index}>{team.teamName}</div>;
});
}
return content;
}

render() {
let Team = this.renderTeamsHandler();
return (
<div>
{Team}
</div>
);
}
}

export default App;


The Ajax call does set data to prevState.Data but by the time it gets to rendering it, the state is the same as before the Ajax call. It is very confused as this all looks correct to me. Is it potentially async issue? If that is the case, why previously what I've done calls like this and had no issue at all.



Thanks for any help in advance.










share|improve this question
























  • I think it's because you are setting value on state.teams instead of state.league.teams.
    – klvenky
    Nov 22 at 8:20












  • Add a break point inside your success and step into your code. Once you've hit your break point step inside the function call and see whats going on.
    – Luke Walker
    Nov 22 at 8:23










  • "let prevState = [...this.state.league.teams];". Shouldn't it be "let prevState = {...this.state.league.teams};" (notice curly braces)
    – Anton Pastukhov
    Nov 22 at 9:21













up vote
0
down vote

favorite









up vote
0
down vote

favorite











import React, { Component} from 'react';
import { Route } from 'react-router';
import axios from 'axios';

class App extends Component {
constructor(props) {
super(props);
this.state = {
league: {
teams: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
players: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
games: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
error: false
}
};
}

componentDidMount() {
this.getTeamsHandler();
}

getTeamsHandler = () => {
axios.get('/api/League/GetTeams')
.then((response) => {
let prevState = [...this.state.league.teams];
prevState.data = response.data;
prevState.loaded = true;
this.setState({ teams: prevState });
})
.catch((error) => {
this.setState({ error: error });
});
}

renderTeamsHandler = () => {
let games = this.state.league.games;
let content = null;
if (games.data.length > 0) {
content = games.data.map((team, index) => {
return <div key={index}>{team.teamName}</div>;
});
}
return content;
}

render() {
let Team = this.renderTeamsHandler();
return (
<div>
{Team}
</div>
);
}
}

export default App;


The Ajax call does set data to prevState.Data but by the time it gets to rendering it, the state is the same as before the Ajax call. It is very confused as this all looks correct to me. Is it potentially async issue? If that is the case, why previously what I've done calls like this and had no issue at all.



Thanks for any help in advance.










share|improve this question















import React, { Component} from 'react';
import { Route } from 'react-router';
import axios from 'axios';

class App extends Component {
constructor(props) {
super(props);
this.state = {
league: {
teams: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
players: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
games: {
data: ,
loaded: false,
config: {
icon: true,
parentId: 'leftSideTreeView'
}
},
error: false
}
};
}

componentDidMount() {
this.getTeamsHandler();
}

getTeamsHandler = () => {
axios.get('/api/League/GetTeams')
.then((response) => {
let prevState = [...this.state.league.teams];
prevState.data = response.data;
prevState.loaded = true;
this.setState({ teams: prevState });
})
.catch((error) => {
this.setState({ error: error });
});
}

renderTeamsHandler = () => {
let games = this.state.league.games;
let content = null;
if (games.data.length > 0) {
content = games.data.map((team, index) => {
return <div key={index}>{team.teamName}</div>;
});
}
return content;
}

render() {
let Team = this.renderTeamsHandler();
return (
<div>
{Team}
</div>
);
}
}

export default App;


The Ajax call does set data to prevState.Data but by the time it gets to rendering it, the state is the same as before the Ajax call. It is very confused as this all looks correct to me. Is it potentially async issue? If that is the case, why previously what I've done calls like this and had no issue at all.



Thanks for any help in advance.







reactjs reactjs.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:54









kit

1,1101416




1,1101416










asked Nov 22 at 8:04









N.E.Webber

95




95












  • I think it's because you are setting value on state.teams instead of state.league.teams.
    – klvenky
    Nov 22 at 8:20












  • Add a break point inside your success and step into your code. Once you've hit your break point step inside the function call and see whats going on.
    – Luke Walker
    Nov 22 at 8:23










  • "let prevState = [...this.state.league.teams];". Shouldn't it be "let prevState = {...this.state.league.teams};" (notice curly braces)
    – Anton Pastukhov
    Nov 22 at 9:21


















  • I think it's because you are setting value on state.teams instead of state.league.teams.
    – klvenky
    Nov 22 at 8:20












  • Add a break point inside your success and step into your code. Once you've hit your break point step inside the function call and see whats going on.
    – Luke Walker
    Nov 22 at 8:23










  • "let prevState = [...this.state.league.teams];". Shouldn't it be "let prevState = {...this.state.league.teams};" (notice curly braces)
    – Anton Pastukhov
    Nov 22 at 9:21
















I think it's because you are setting value on state.teams instead of state.league.teams.
– klvenky
Nov 22 at 8:20






I think it's because you are setting value on state.teams instead of state.league.teams.
– klvenky
Nov 22 at 8:20














Add a break point inside your success and step into your code. Once you've hit your break point step inside the function call and see whats going on.
– Luke Walker
Nov 22 at 8:23




Add a break point inside your success and step into your code. Once you've hit your break point step inside the function call and see whats going on.
– Luke Walker
Nov 22 at 8:23












"let prevState = [...this.state.league.teams];". Shouldn't it be "let prevState = {...this.state.league.teams};" (notice curly braces)
– Anton Pastukhov
Nov 22 at 9:21




"let prevState = [...this.state.league.teams];". Shouldn't it be "let prevState = {...this.state.league.teams};" (notice curly braces)
– Anton Pastukhov
Nov 22 at 9:21












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I suspect that there are two part of problems.



First,the setState in getTeamsHandler:



axios.get('/api/League/GetTeams')
.then((response) => {
let prevTeam = [...this.state.league.teams];
prevTeam.data = response.data;
prevTeam.loaded = true;
this.setState(prevState => ({
league: {
...prevState.league,
teams: prevTeam
}
})
})
.catch((error) => {
this.setState(prevState => ({
league: {
...prevState.league,
error: error
}
});
});


Second,I guess there are some mistakes in renderTeamsHandler.Fetch date and set them in team, but use group in renderTeamsHandler.And the group in state is .



renderTeamsHandler = () => {
let teams = this.state.league.teams;
let content = null;
if (teams.data.length > 0) {
content = teams.data.map((team, index) => {
return <div key={index}>{team.teamName}</div>;
});
}

return content;
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426329%2freact-js-componentdidmount-ajax-call-setstate-does-not-update-state%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I suspect that there are two part of problems.



    First,the setState in getTeamsHandler:



    axios.get('/api/League/GetTeams')
    .then((response) => {
    let prevTeam = [...this.state.league.teams];
    prevTeam.data = response.data;
    prevTeam.loaded = true;
    this.setState(prevState => ({
    league: {
    ...prevState.league,
    teams: prevTeam
    }
    })
    })
    .catch((error) => {
    this.setState(prevState => ({
    league: {
    ...prevState.league,
    error: error
    }
    });
    });


    Second,I guess there are some mistakes in renderTeamsHandler.Fetch date and set them in team, but use group in renderTeamsHandler.And the group in state is .



    renderTeamsHandler = () => {
    let teams = this.state.league.teams;
    let content = null;
    if (teams.data.length > 0) {
    content = teams.data.map((team, index) => {
    return <div key={index}>{team.teamName}</div>;
    });
    }

    return content;
    }





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      I suspect that there are two part of problems.



      First,the setState in getTeamsHandler:



      axios.get('/api/League/GetTeams')
      .then((response) => {
      let prevTeam = [...this.state.league.teams];
      prevTeam.data = response.data;
      prevTeam.loaded = true;
      this.setState(prevState => ({
      league: {
      ...prevState.league,
      teams: prevTeam
      }
      })
      })
      .catch((error) => {
      this.setState(prevState => ({
      league: {
      ...prevState.league,
      error: error
      }
      });
      });


      Second,I guess there are some mistakes in renderTeamsHandler.Fetch date and set them in team, but use group in renderTeamsHandler.And the group in state is .



      renderTeamsHandler = () => {
      let teams = this.state.league.teams;
      let content = null;
      if (teams.data.length > 0) {
      content = teams.data.map((team, index) => {
      return <div key={index}>{team.teamName}</div>;
      });
      }

      return content;
      }





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        I suspect that there are two part of problems.



        First,the setState in getTeamsHandler:



        axios.get('/api/League/GetTeams')
        .then((response) => {
        let prevTeam = [...this.state.league.teams];
        prevTeam.data = response.data;
        prevTeam.loaded = true;
        this.setState(prevState => ({
        league: {
        ...prevState.league,
        teams: prevTeam
        }
        })
        })
        .catch((error) => {
        this.setState(prevState => ({
        league: {
        ...prevState.league,
        error: error
        }
        });
        });


        Second,I guess there are some mistakes in renderTeamsHandler.Fetch date and set them in team, but use group in renderTeamsHandler.And the group in state is .



        renderTeamsHandler = () => {
        let teams = this.state.league.teams;
        let content = null;
        if (teams.data.length > 0) {
        content = teams.data.map((team, index) => {
        return <div key={index}>{team.teamName}</div>;
        });
        }

        return content;
        }





        share|improve this answer












        I suspect that there are two part of problems.



        First,the setState in getTeamsHandler:



        axios.get('/api/League/GetTeams')
        .then((response) => {
        let prevTeam = [...this.state.league.teams];
        prevTeam.data = response.data;
        prevTeam.loaded = true;
        this.setState(prevState => ({
        league: {
        ...prevState.league,
        teams: prevTeam
        }
        })
        })
        .catch((error) => {
        this.setState(prevState => ({
        league: {
        ...prevState.league,
        error: error
        }
        });
        });


        Second,I guess there are some mistakes in renderTeamsHandler.Fetch date and set them in team, but use group in renderTeamsHandler.And the group in state is .



        renderTeamsHandler = () => {
        let teams = this.state.league.teams;
        let content = null;
        if (teams.data.length > 0) {
        content = teams.data.map((team, index) => {
        return <div key={index}>{team.teamName}</div>;
        });
        }

        return content;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 8:53









        Root

        1,130128




        1,130128






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426329%2freact-js-componentdidmount-ajax-call-setstate-does-not-update-state%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Catalogne

            Violoncelliste

            Héron pourpré