React state contains correct data initially but is overwritten by previous value2019 Community Moderator ElectionCorrect modification of state arrays in ReactJSWhat is the difference between state and props in React?React js onClick can't pass value to methodjson data as react state is not updatedHow get value datapicker in react toobox custom?Update React Grandparent State from onClick in GrandchildReact fetch data getting lostReact Concat StatesetState also changes screen propsfetch data in react native from mlab
They call me Inspector Morse
How do I deal with a powergamer in a game full of beginners in a school club?
Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?
Does "variables should live in the smallest scope as possible" include the case "variables should not exist if possible"?
Why does the negative sign arise in this thermodynamic relation?
The bar has been raised
In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?
Solving "Resistance between two nodes on a grid" problem in Mathematica
Do f-stop and exposure time perfectly cancel?
Regex for certain words causes Spaces
Why the color red for the Republican Party
What is the likely impact of grounding an entire aircraft series?
Is there any way to damage Intellect Devourer(s) when already within a creature's skull?
Word for a person who has no opinion about whether god exists
Good for you! in Russian
"One can do his homework in the library"
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Is there an equal sign with wider gap?
Accountant/ lawyer will not return my call
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Why is Beresheet doing a only a one-way trip?
Best approach to update all entries in a list that is paginated?
Are babies of evil humanoid species inherently evil?
If the Captain's screens are out, does he switch seats with the co-pilot?
React state contains correct data initially but is overwritten by previous value
2019 Community Moderator ElectionCorrect modification of state arrays in ReactJSWhat is the difference between state and props in React?React js onClick can't pass value to methodjson data as react state is not updatedHow get value datapicker in react toobox custom?Update React Grandparent State from onClick in GrandchildReact fetch data getting lostReact Concat StatesetState also changes screen propsfetch data in react native from mlab
i have a state variable which is initially set to null. After getting data from server i update its state. In doing so.. it loads correct data for items with different ids. however on opening the item that was previously opened... sets the state with correct value. however it then overwrites the state value with previous opened item.
I am not sure what going wrong.
Below is the code,
class Items extends React.PureComponent
i have a state variable which is initially set to null. After getting data from server i update its state. In doing so.. it loads correct data for items with different ids. however on opening the item that was previously opened... sets the state with correct value. however it then overwrites the state value with previous opened item.
I am not sure what going wrong.
Below is the code,
class Items extends React.PureComponent improve this answer
add a comment
Note that this code will not react to any changes to item_id while load_item_details is fetching data. And it will break on error inside client.get_item_details_file.
add a comment |
You have item_id inside method 1 and item_id inside method 8 in the second log. Looks like you send two simultaneous requests with different ids. Since data for item_id=1 is much larger than data for item_id=8 it arrives later and overwrites everything. Try to call load_item_details only when needed and use some flag to block simultaneous requests.
...
this.state =
item_details: null,
isLoading: false,
;
...
componentDidUpdate(PrevProps)
if (PrevProps.item_id !== this.props.item_id) this.load_item_details();
...
load_item_details = () =>
if (this.state.isLoading) return;
const file_name = 'item_details.json';
this.setState(isLoading: true);
client.get_item_details_file(this.props.item_id, file_name, 'json')
.then((request) =>
this.setState(item_details: request.response, isLoading: false);
);
}
Note that this code will not react to any changes to item_id while load_item_details is fetching data. And it will break on error inside client.get_item_details_file.
You have item_id inside method 1 and item_id inside method 8 in the second log. Looks like you send two simultaneous requests with different ids. Since data for item_id=1 is much larger than data for item_id=8 it arrives later and overwrites everything. Try to call load_item_details only when needed and use some flag to block simultaneous requests.
...
this.state =
item_details: null,
isLoading: false,
;
...
componentDidUpdate(PrevProps)
if (PrevProps.item_id !== this.props.item_id) this.load_item_details();
...
load_item_details = () =>
if (this.state.isLoading) return;
const file_name = 'item_details.json';
this.setState(isLoading: true);
client.get_item_details_file(this.props.item_id, file_name, 'json')
.then((request) =>
this.setState(item_details: request.response, isLoading: false);
);
}
Note that this code will not react to any changes to item_id while load_item_details is fetching data. And it will break on error inside client.get_item_details_file.
answered Mar 6 at 18:28
UjinT34UjinT34
78411
78411
add a comment |
add a comment |
You probably have a typo on your code:
load_item_details = () =>
const file_name = 'item_details.json';
client.get_item_details_file(this.props.item_id, file_name, 'json')
.then((request) =>
this.setState(item_detail: request.response);
);
}
It should be this.setState(item_details: request.response)
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23
add a comment |
You probably have a typo on your code:
load_item_details = () =>
const file_name = 'item_details.json';
client.get_item_details_file(this.props.item_id, file_name, 'json')
.then((request) =>
this.setState(item_detail: request.response);
);
}
It should be this.setState(item_details: request.response)
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23
add a comment |
You probably have a typo on your code:
load_item_details = () =>
const file_name = 'item_details.json';
client.get_item_details_file(this.props.item_id, file_name, 'json')
.then((request) =>
this.setState(item_detail: request.response);
);
}
It should be this.setState(item_details: request.response)
You probably have a typo on your code:
load_item_details = () =>
const file_name = 'item_details.json';
client.get_item_details_file(this.props.item_id, file_name, 'json')
.then((request) =>
this.setState(item_detail: request.response);
);
}
It should be this.setState(item_details: request.response)
edited Mar 6 at 16:19
kiranvj
12.9k23453
12.9k23453
answered Mar 6 at 16:18
LuisMendes535LuisMendes535
10317
10317
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23
add a comment |
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23
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.
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%2f55027591%2freact-state-contains-correct-data-initially-but-is-overwritten-by-previous-value%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
its a typo...although i mean the state value is overwritten by previous value.
– stackoverflow_user
Mar 6 at 16:23