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.
reactjs reactjs.net
add a comment |
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.
reactjs reactjs.net
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
add a comment |
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.
reactjs reactjs.net
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
reactjs reactjs.net
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
add a comment |
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 22 at 8:53
Root
1,130128
1,130128
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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