What is the best approach to work with nested object in this.state? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to update nested state properties in ReactWhat is the most efficient way to deep clone an object in JavaScript?What is the best way to add options to a select from as a JS object with jQuery?What's the best way to break from nested (for) loops?Test for existence of nested JavaScript object keyWhat is the best way to detect a mobile device in jQuery?What is the best way to initialize a JavaScript Date to midnight?Access / process (nested) objects, arrays or JSONUsage of mutable objects inside React Native + Redux appHow to correctly handle data object from REST API using fetch in ReactUnexpected outcome with merging react state
What is "Lambda" in Heston's original paper on stochastic volatility models?
Fit odd number of triplets in a measure?
Is there a verb for listening stealthily?
Did John Wesley plagiarize Matthew Henry...?
Plotting a Maclaurin series
How many time has Arya actually used Needle?
Table formatting with tabularx?
Where did Ptolemy compare the Earth to the distance of fixed stars?
Did any compiler fully use 80-bit floating point?
How do I find my Spellcasting Ability for my D&D character?
Do i imagine the linear (straight line) homotopy in a correct way?
Is there a spell that can create a permanent fire?
Google .dev domain strangely redirects to https
What is the proper term for etching or digging of wall to hide conduit of cables
How to ask rejected full-time candidates to apply to teach individual courses?
How to resize main filesystem
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Noise in Eigenvalues plot
Did pre-Columbian Americans know the spherical shape of the Earth?
Why can't fire hurt Daenerys but it did to Jon Snow in season 1?
Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?
3D Masyu - A Die
How to name indistinguishable henchmen in a screenplay?
Meaning of 境 in その日を境に
What is the best approach to work with nested object in this.state?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to update nested state properties in ReactWhat is the most efficient way to deep clone an object in JavaScript?What is the best way to add options to a select from as a JS object with jQuery?What's the best way to break from nested (for) loops?Test for existence of nested JavaScript object keyWhat is the best way to detect a mobile device in jQuery?What is the best way to initialize a JavaScript Date to midnight?Access / process (nested) objects, arrays or JSONUsage of mutable objects inside React Native + Redux appHow to correctly handle data object from REST API using fetch in ReactUnexpected outcome with merging react state
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I was reading the react documentation State Updates are Merged and theres a part it says when we set a object like this eg: this.setState(comments)
it completely replaces this.state.comments
.
So, I wonder if the approach to change name
of user
object on event onChangeText
is valid, because the whole object it'll be replaced everytime onChangeText
is called.
Is that a good practice or have side effects like bad perfomance?
example code:
this.state =
user:
name: "",
surname: "",
,
isLoading: false
;
/***/
onChangeText=name => this.setState(
user:
...user,
name
)
javascript reactjs react-native
add a comment |
I was reading the react documentation State Updates are Merged and theres a part it says when we set a object like this eg: this.setState(comments)
it completely replaces this.state.comments
.
So, I wonder if the approach to change name
of user
object on event onChangeText
is valid, because the whole object it'll be replaced everytime onChangeText
is called.
Is that a good practice or have side effects like bad perfomance?
example code:
this.state =
user:
name: "",
surname: "",
,
isLoading: false
;
/***/
onChangeText=name => this.setState(
user:
...user,
name
)
javascript reactjs react-native
take a look at: stackoverflow.com/a/51136076/5066625
– Miroslav Glamuzina
Mar 9 at 1:10
add a comment |
I was reading the react documentation State Updates are Merged and theres a part it says when we set a object like this eg: this.setState(comments)
it completely replaces this.state.comments
.
So, I wonder if the approach to change name
of user
object on event onChangeText
is valid, because the whole object it'll be replaced everytime onChangeText
is called.
Is that a good practice or have side effects like bad perfomance?
example code:
this.state =
user:
name: "",
surname: "",
,
isLoading: false
;
/***/
onChangeText=name => this.setState(
user:
...user,
name
)
javascript reactjs react-native
I was reading the react documentation State Updates are Merged and theres a part it says when we set a object like this eg: this.setState(comments)
it completely replaces this.state.comments
.
So, I wonder if the approach to change name
of user
object on event onChangeText
is valid, because the whole object it'll be replaced everytime onChangeText
is called.
Is that a good practice or have side effects like bad perfomance?
example code:
this.state =
user:
name: "",
surname: "",
,
isLoading: false
;
/***/
onChangeText=name => this.setState(
user:
...user,
name
)
this.state =
user:
name: "",
surname: "",
,
isLoading: false
;
/***/
onChangeText=name => this.setState(
user:
...user,
name
)
this.state =
user:
name: "",
surname: "",
,
isLoading: false
;
/***/
onChangeText=name => this.setState(
user:
...user,
name
)
javascript reactjs react-native
javascript reactjs react-native
edited Mar 9 at 11:34
Eduardo Breno
asked Mar 9 at 1:02
Eduardo BrenoEduardo Breno
1135
1135
take a look at: stackoverflow.com/a/51136076/5066625
– Miroslav Glamuzina
Mar 9 at 1:10
add a comment |
take a look at: stackoverflow.com/a/51136076/5066625
– Miroslav Glamuzina
Mar 9 at 1:10
take a look at: stackoverflow.com/a/51136076/5066625
– Miroslav Glamuzina
Mar 9 at 1:10
take a look at: stackoverflow.com/a/51136076/5066625
– Miroslav Glamuzina
Mar 9 at 1:10
add a comment |
3 Answers
3
active
oldest
votes
Inmutability is one point performance is another.
Inmutability meas that you should not use the same reference to update an object but to replace with a new one. In React this is recommended because plenty of updates only happen if the object is new. You could create a problem if you re-use the user
in the state, change the name and then update the state with it. And that is why it is a recommendation, in most cases React magic will work on others it can't tell the difference. So you would have to
const copy = Object.assign(, this.state.user);
You can see this explanation here in the power of not mutating data
Performance in this case is concerning you because is a text listener, nevermind about it, React keeps track of the UI in a virtual DOM so it can update only what is different. This means everything will remain the same except for the new or deleted character.
You can try the clock example in the doc for that.
add a comment |
I'd recommend set
from lodash/fp
:
https://gist.github.com/jfmengels/6b973b69c491375117dc#_setpath-value-object
add a comment |
Your example is perfectly valid, provided the user
variable is up-to-date.
To ensure that, you might want to use the function arg form of setState
. This will call your function with the current value of this.state
and set state to the returned value.
onChangeText=
name =>
this.setState(
( user ) => (
user: ...user, name
)
)
I understand, but i don't think is a good pratice replace whole object every timeonChangeText
is called.
– Eduardo Breno
Mar 9 at 11:37
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap theuser
object for a new object, but keep the same references for it's key values, except for name which you change.
– lecstor
Mar 9 at 11:41
actually, I'm thinking about PureComponent/Memo shallow prop checking. i thinksetState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.
– lecstor
Mar 9 at 11:44
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55072981%2fwhat-is-the-best-approach-to-work-with-nested-object-in-this-state%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Inmutability is one point performance is another.
Inmutability meas that you should not use the same reference to update an object but to replace with a new one. In React this is recommended because plenty of updates only happen if the object is new. You could create a problem if you re-use the user
in the state, change the name and then update the state with it. And that is why it is a recommendation, in most cases React magic will work on others it can't tell the difference. So you would have to
const copy = Object.assign(, this.state.user);
You can see this explanation here in the power of not mutating data
Performance in this case is concerning you because is a text listener, nevermind about it, React keeps track of the UI in a virtual DOM so it can update only what is different. This means everything will remain the same except for the new or deleted character.
You can try the clock example in the doc for that.
add a comment |
Inmutability is one point performance is another.
Inmutability meas that you should not use the same reference to update an object but to replace with a new one. In React this is recommended because plenty of updates only happen if the object is new. You could create a problem if you re-use the user
in the state, change the name and then update the state with it. And that is why it is a recommendation, in most cases React magic will work on others it can't tell the difference. So you would have to
const copy = Object.assign(, this.state.user);
You can see this explanation here in the power of not mutating data
Performance in this case is concerning you because is a text listener, nevermind about it, React keeps track of the UI in a virtual DOM so it can update only what is different. This means everything will remain the same except for the new or deleted character.
You can try the clock example in the doc for that.
add a comment |
Inmutability is one point performance is another.
Inmutability meas that you should not use the same reference to update an object but to replace with a new one. In React this is recommended because plenty of updates only happen if the object is new. You could create a problem if you re-use the user
in the state, change the name and then update the state with it. And that is why it is a recommendation, in most cases React magic will work on others it can't tell the difference. So you would have to
const copy = Object.assign(, this.state.user);
You can see this explanation here in the power of not mutating data
Performance in this case is concerning you because is a text listener, nevermind about it, React keeps track of the UI in a virtual DOM so it can update only what is different. This means everything will remain the same except for the new or deleted character.
You can try the clock example in the doc for that.
Inmutability is one point performance is another.
Inmutability meas that you should not use the same reference to update an object but to replace with a new one. In React this is recommended because plenty of updates only happen if the object is new. You could create a problem if you re-use the user
in the state, change the name and then update the state with it. And that is why it is a recommendation, in most cases React magic will work on others it can't tell the difference. So you would have to
const copy = Object.assign(, this.state.user);
You can see this explanation here in the power of not mutating data
Performance in this case is concerning you because is a text listener, nevermind about it, React keeps track of the UI in a virtual DOM so it can update only what is different. This means everything will remain the same except for the new or deleted character.
You can try the clock example in the doc for that.
answered Mar 9 at 1:12
cutikocutiko
3,17321934
3,17321934
add a comment |
add a comment |
I'd recommend set
from lodash/fp
:
https://gist.github.com/jfmengels/6b973b69c491375117dc#_setpath-value-object
add a comment |
I'd recommend set
from lodash/fp
:
https://gist.github.com/jfmengels/6b973b69c491375117dc#_setpath-value-object
add a comment |
I'd recommend set
from lodash/fp
:
https://gist.github.com/jfmengels/6b973b69c491375117dc#_setpath-value-object
I'd recommend set
from lodash/fp
:
https://gist.github.com/jfmengels/6b973b69c491375117dc#_setpath-value-object
answered Mar 9 at 1:13
MarkusMarkus
1,21311230
1,21311230
add a comment |
add a comment |
Your example is perfectly valid, provided the user
variable is up-to-date.
To ensure that, you might want to use the function arg form of setState
. This will call your function with the current value of this.state
and set state to the returned value.
onChangeText=
name =>
this.setState(
( user ) => (
user: ...user, name
)
)
I understand, but i don't think is a good pratice replace whole object every timeonChangeText
is called.
– Eduardo Breno
Mar 9 at 11:37
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap theuser
object for a new object, but keep the same references for it's key values, except for name which you change.
– lecstor
Mar 9 at 11:41
actually, I'm thinking about PureComponent/Memo shallow prop checking. i thinksetState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.
– lecstor
Mar 9 at 11:44
add a comment |
Your example is perfectly valid, provided the user
variable is up-to-date.
To ensure that, you might want to use the function arg form of setState
. This will call your function with the current value of this.state
and set state to the returned value.
onChangeText=
name =>
this.setState(
( user ) => (
user: ...user, name
)
)
I understand, but i don't think is a good pratice replace whole object every timeonChangeText
is called.
– Eduardo Breno
Mar 9 at 11:37
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap theuser
object for a new object, but keep the same references for it's key values, except for name which you change.
– lecstor
Mar 9 at 11:41
actually, I'm thinking about PureComponent/Memo shallow prop checking. i thinksetState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.
– lecstor
Mar 9 at 11:44
add a comment |
Your example is perfectly valid, provided the user
variable is up-to-date.
To ensure that, you might want to use the function arg form of setState
. This will call your function with the current value of this.state
and set state to the returned value.
onChangeText=
name =>
this.setState(
( user ) => (
user: ...user, name
)
)
Your example is perfectly valid, provided the user
variable is up-to-date.
To ensure that, you might want to use the function arg form of setState
. This will call your function with the current value of this.state
and set state to the returned value.
onChangeText=
name =>
this.setState(
( user ) => (
user: ...user, name
)
)
answered Mar 9 at 5:59
lecstorlecstor
4,1161318
4,1161318
I understand, but i don't think is a good pratice replace whole object every timeonChangeText
is called.
– Eduardo Breno
Mar 9 at 11:37
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap theuser
object for a new object, but keep the same references for it's key values, except for name which you change.
– lecstor
Mar 9 at 11:41
actually, I'm thinking about PureComponent/Memo shallow prop checking. i thinksetState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.
– lecstor
Mar 9 at 11:44
add a comment |
I understand, but i don't think is a good pratice replace whole object every timeonChangeText
is called.
– Eduardo Breno
Mar 9 at 11:37
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap theuser
object for a new object, but keep the same references for it's key values, except for name which you change.
– lecstor
Mar 9 at 11:41
actually, I'm thinking about PureComponent/Memo shallow prop checking. i thinksetState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.
– lecstor
Mar 9 at 11:44
I understand, but i don't think is a good pratice replace whole object every time
onChangeText
is called.– Eduardo Breno
Mar 9 at 11:37
I understand, but i don't think is a good pratice replace whole object every time
onChangeText
is called.– Eduardo Breno
Mar 9 at 11:37
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap the
user
object for a new object, but keep the same references for it's key values, except for name which you change.– lecstor
Mar 9 at 11:41
you need to replace the top level object, or React won't detect the change and re-render. It's the premise of immutibility and change detection. With that code you swap the
user
object for a new object, but keep the same references for it's key values, except for name which you change.– lecstor
Mar 9 at 11:41
actually, I'm thinking about PureComponent/Memo shallow prop checking. i think
setState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.– lecstor
Mar 9 at 11:44
actually, I'm thinking about PureComponent/Memo shallow prop checking. i think
setState
triggers re-render regardless. But if you pass those state values to child components that connect to Redux, or are PureComponents, or Memoed, then depending on where they are in the component props, mutating an object may mean re-renders don't always occur when they should.– lecstor
Mar 9 at 11:44
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%2f55072981%2fwhat-is-the-best-approach-to-work-with-nested-object-in-this-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
take a look at: stackoverflow.com/a/51136076/5066625
– Miroslav Glamuzina
Mar 9 at 1:10