Referencing state from within state - React js
Is it possible to reference state from within state to create an id. e.g id = this.state.name + this.state.desc
Something like this?
this.state = {name:'', desc:'', id:`${this.state.name}-${this.state.desc}`}
javascript reactjs
add a comment |
Is it possible to reference state from within state to create an id. e.g id = this.state.name + this.state.desc
Something like this?
this.state = {name:'', desc:'', id:`${this.state.name}-${this.state.desc}`}
javascript reactjs
1
What happens when you try it?
– Herohtar
Nov 28 '18 at 22:52
Depends where are you doing it. If within a class body, you will receive an error.
– kind user
Nov 28 '18 at 22:53
I think you can't since state is not yet initialized. But you will know where to get those values, so you can simply set id when your state is updated
– soulpaul
Nov 28 '18 at 22:54
i think it's possible if you're writing multiple assignments in the constructor, but i wouldn't do it. it feels like an anti-pattern, but without knowing the whole context idk what to suggest here. (this also feels more like a code review question than SE)
– worc
Nov 28 '18 at 23:06
add a comment |
Is it possible to reference state from within state to create an id. e.g id = this.state.name + this.state.desc
Something like this?
this.state = {name:'', desc:'', id:`${this.state.name}-${this.state.desc}`}
javascript reactjs
Is it possible to reference state from within state to create an id. e.g id = this.state.name + this.state.desc
Something like this?
this.state = {name:'', desc:'', id:`${this.state.name}-${this.state.desc}`}
javascript reactjs
javascript reactjs
edited Nov 29 '18 at 0:13
worc
1,58621928
1,58621928
asked Nov 28 '18 at 22:50
George BleasdaleGeorge Bleasdale
436
436
1
What happens when you try it?
– Herohtar
Nov 28 '18 at 22:52
Depends where are you doing it. If within a class body, you will receive an error.
– kind user
Nov 28 '18 at 22:53
I think you can't since state is not yet initialized. But you will know where to get those values, so you can simply set id when your state is updated
– soulpaul
Nov 28 '18 at 22:54
i think it's possible if you're writing multiple assignments in the constructor, but i wouldn't do it. it feels like an anti-pattern, but without knowing the whole context idk what to suggest here. (this also feels more like a code review question than SE)
– worc
Nov 28 '18 at 23:06
add a comment |
1
What happens when you try it?
– Herohtar
Nov 28 '18 at 22:52
Depends where are you doing it. If within a class body, you will receive an error.
– kind user
Nov 28 '18 at 22:53
I think you can't since state is not yet initialized. But you will know where to get those values, so you can simply set id when your state is updated
– soulpaul
Nov 28 '18 at 22:54
i think it's possible if you're writing multiple assignments in the constructor, but i wouldn't do it. it feels like an anti-pattern, but without knowing the whole context idk what to suggest here. (this also feels more like a code review question than SE)
– worc
Nov 28 '18 at 23:06
1
1
What happens when you try it?
– Herohtar
Nov 28 '18 at 22:52
What happens when you try it?
– Herohtar
Nov 28 '18 at 22:52
Depends where are you doing it. If within a class body, you will receive an error.
– kind user
Nov 28 '18 at 22:53
Depends where are you doing it. If within a class body, you will receive an error.
– kind user
Nov 28 '18 at 22:53
I think you can't since state is not yet initialized. But you will know where to get those values, so you can simply set id when your state is updated
– soulpaul
Nov 28 '18 at 22:54
I think you can't since state is not yet initialized. But you will know where to get those values, so you can simply set id when your state is updated
– soulpaul
Nov 28 '18 at 22:54
i think it's possible if you're writing multiple assignments in the constructor, but i wouldn't do it. it feels like an anti-pattern, but without knowing the whole context idk what to suggest here. (this also feels more like a code review question than SE)
– worc
Nov 28 '18 at 23:06
i think it's possible if you're writing multiple assignments in the constructor, but i wouldn't do it. it feels like an anti-pattern, but without knowing the whole context idk what to suggest here. (this also feels more like a code review question than SE)
– worc
Nov 28 '18 at 23:06
add a comment |
2 Answers
2
active
oldest
votes
Remember you need to set the state using setState
So
this.setState({ id:`${this.state.name}-${this.state.desc}` })
This is also assuming that you have already set the state in the constructor (or somewhere else int he app).
If you are in the constructor, then this should work just fine...
this.state = { name: 'Toli', desc: 'A programmer' };
this.state = {...this.state, id:`${this.state.name}-${this.state.desc}`}
I'm assuming you want to keep name and desc in the state so I'm copying them using the spread op (...this.state)
1
you can, and should, set state withoutsetStatein the constructor
– worc
Nov 28 '18 at 22:58
1
@worc - but if the OP is trying to usethis.state = {....}and referencethis.stateinside it, then they are obviously not doing it in the constructor as the state has been previously defined.
– Adam
Nov 28 '18 at 23:00
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
you could have multiple lines ofthis.state = ...and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.
– worc
Nov 28 '18 at 23:03
add a comment |
Nope, that won't work. this.state is initialized before the constructor, but it won't like that you are referencing an undefined portion of the object. You would need to set it after initialization. I'm assuming you would be using this.state = {blah} from your constructor. Otherwise, you should use this.setState().
However this is also bad practice because anytime you update your name or desc, it will update the state again with your new id value. I don't know the full scope of your code, but you can probably just save the id to a function that gives you the string.
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%2f53529289%2freferencing-state-from-within-state-react-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Remember you need to set the state using setState
So
this.setState({ id:`${this.state.name}-${this.state.desc}` })
This is also assuming that you have already set the state in the constructor (or somewhere else int he app).
If you are in the constructor, then this should work just fine...
this.state = { name: 'Toli', desc: 'A programmer' };
this.state = {...this.state, id:`${this.state.name}-${this.state.desc}`}
I'm assuming you want to keep name and desc in the state so I'm copying them using the spread op (...this.state)
1
you can, and should, set state withoutsetStatein the constructor
– worc
Nov 28 '18 at 22:58
1
@worc - but if the OP is trying to usethis.state = {....}and referencethis.stateinside it, then they are obviously not doing it in the constructor as the state has been previously defined.
– Adam
Nov 28 '18 at 23:00
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
you could have multiple lines ofthis.state = ...and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.
– worc
Nov 28 '18 at 23:03
add a comment |
Remember you need to set the state using setState
So
this.setState({ id:`${this.state.name}-${this.state.desc}` })
This is also assuming that you have already set the state in the constructor (or somewhere else int he app).
If you are in the constructor, then this should work just fine...
this.state = { name: 'Toli', desc: 'A programmer' };
this.state = {...this.state, id:`${this.state.name}-${this.state.desc}`}
I'm assuming you want to keep name and desc in the state so I'm copying them using the spread op (...this.state)
1
you can, and should, set state withoutsetStatein the constructor
– worc
Nov 28 '18 at 22:58
1
@worc - but if the OP is trying to usethis.state = {....}and referencethis.stateinside it, then they are obviously not doing it in the constructor as the state has been previously defined.
– Adam
Nov 28 '18 at 23:00
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
you could have multiple lines ofthis.state = ...and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.
– worc
Nov 28 '18 at 23:03
add a comment |
Remember you need to set the state using setState
So
this.setState({ id:`${this.state.name}-${this.state.desc}` })
This is also assuming that you have already set the state in the constructor (or somewhere else int he app).
If you are in the constructor, then this should work just fine...
this.state = { name: 'Toli', desc: 'A programmer' };
this.state = {...this.state, id:`${this.state.name}-${this.state.desc}`}
I'm assuming you want to keep name and desc in the state so I'm copying them using the spread op (...this.state)
Remember you need to set the state using setState
So
this.setState({ id:`${this.state.name}-${this.state.desc}` })
This is also assuming that you have already set the state in the constructor (or somewhere else int he app).
If you are in the constructor, then this should work just fine...
this.state = { name: 'Toli', desc: 'A programmer' };
this.state = {...this.state, id:`${this.state.name}-${this.state.desc}`}
I'm assuming you want to keep name and desc in the state so I'm copying them using the spread op (...this.state)
edited Nov 28 '18 at 23:06
answered Nov 28 '18 at 22:56
ToliToli
2,13141935
2,13141935
1
you can, and should, set state withoutsetStatein the constructor
– worc
Nov 28 '18 at 22:58
1
@worc - but if the OP is trying to usethis.state = {....}and referencethis.stateinside it, then they are obviously not doing it in the constructor as the state has been previously defined.
– Adam
Nov 28 '18 at 23:00
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
you could have multiple lines ofthis.state = ...and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.
– worc
Nov 28 '18 at 23:03
add a comment |
1
you can, and should, set state withoutsetStatein the constructor
– worc
Nov 28 '18 at 22:58
1
@worc - but if the OP is trying to usethis.state = {....}and referencethis.stateinside it, then they are obviously not doing it in the constructor as the state has been previously defined.
– Adam
Nov 28 '18 at 23:00
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
you could have multiple lines ofthis.state = ...and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.
– worc
Nov 28 '18 at 23:03
1
1
you can, and should, set state without
setState in the constructor– worc
Nov 28 '18 at 22:58
you can, and should, set state without
setState in the constructor– worc
Nov 28 '18 at 22:58
1
1
@worc - but if the OP is trying to use
this.state = {....} and reference this.state inside it, then they are obviously not doing it in the constructor as the state has been previously defined.– Adam
Nov 28 '18 at 23:00
@worc - but if the OP is trying to use
this.state = {....} and reference this.state inside it, then they are obviously not doing it in the constructor as the state has been previously defined.– Adam
Nov 28 '18 at 23:00
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
I think that's the point. He is trying to do it in the constructor.
– soulpaul
Nov 28 '18 at 23:03
you could have multiple lines of
this.state = ... and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.– worc
Nov 28 '18 at 23:03
you could have multiple lines of
this.state = ... and then i think you could technically do what they're asking. it wouldn't be very idiomatic though.– worc
Nov 28 '18 at 23:03
add a comment |
Nope, that won't work. this.state is initialized before the constructor, but it won't like that you are referencing an undefined portion of the object. You would need to set it after initialization. I'm assuming you would be using this.state = {blah} from your constructor. Otherwise, you should use this.setState().
However this is also bad practice because anytime you update your name or desc, it will update the state again with your new id value. I don't know the full scope of your code, but you can probably just save the id to a function that gives you the string.
add a comment |
Nope, that won't work. this.state is initialized before the constructor, but it won't like that you are referencing an undefined portion of the object. You would need to set it after initialization. I'm assuming you would be using this.state = {blah} from your constructor. Otherwise, you should use this.setState().
However this is also bad practice because anytime you update your name or desc, it will update the state again with your new id value. I don't know the full scope of your code, but you can probably just save the id to a function that gives you the string.
add a comment |
Nope, that won't work. this.state is initialized before the constructor, but it won't like that you are referencing an undefined portion of the object. You would need to set it after initialization. I'm assuming you would be using this.state = {blah} from your constructor. Otherwise, you should use this.setState().
However this is also bad practice because anytime you update your name or desc, it will update the state again with your new id value. I don't know the full scope of your code, but you can probably just save the id to a function that gives you the string.
Nope, that won't work. this.state is initialized before the constructor, but it won't like that you are referencing an undefined portion of the object. You would need to set it after initialization. I'm assuming you would be using this.state = {blah} from your constructor. Otherwise, you should use this.setState().
However this is also bad practice because anytime you update your name or desc, it will update the state again with your new id value. I don't know the full scope of your code, but you can probably just save the id to a function that gives you the string.
answered Nov 28 '18 at 23:01
NathanNathan
37913
37913
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.
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%2f53529289%2freferencing-state-from-within-state-react-js%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
1
What happens when you try it?
– Herohtar
Nov 28 '18 at 22:52
Depends where are you doing it. If within a class body, you will receive an error.
– kind user
Nov 28 '18 at 22:53
I think you can't since state is not yet initialized. But you will know where to get those values, so you can simply set id when your state is updated
– soulpaul
Nov 28 '18 at 22:54
i think it's possible if you're writing multiple assignments in the constructor, but i wouldn't do it. it feels like an anti-pattern, but without knowing the whole context idk what to suggest here. (this also feels more like a code review question than SE)
– worc
Nov 28 '18 at 23:06