Correct variable syntax in this.state [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
react setState with dynamic key
(1 answer)
Reactjs setState() with a dynamic key name?
(13 answers)
Closed 4 years ago.
I am trying to set variable in this.state
My variable is defined as VariableName+"Error"
This code works well, but it isnt good to mutate state directly:
this.state[VariableName+"Error"] = "";
My bad try to mutate state in this.state which does not work:
this.setState({
VariableNameError: ""
})

Related

how to destruct this object with customer:id [duplicate]

This question already has answers here:
How to destructure object properties with key names that are invalid variable names?
(3 answers)
How to use special characters (like hyphen) in destructuring assignment syntax? [duplicate]
(2 answers)
Closed 7 months ago.
I have an object with this data structure returned by api:
const obj = {customer:id: '123'}
How should I destruct this object? Thanks

What does this constant definition mean [duplicate]

This question already has answers here:
What is destructuring assignment and its uses?
(3 answers)
Closed 8 months ago.
What does this assignment mean?
const { data: scoreData } = useMostRecentScore(studentId, loginId)
If useMostRecentScore returns an object, with a data property, it will create a new variable called scoreData and assign the contents of the data property.

What is this method carrying out in React.js? [duplicate]

This question already has answers here:
Square Brackets Javascript Object Key
(5 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
react setState with dynamic key
(1 answer)
Closed 4 years ago.
I'm reading through how to create login forms & came across this method:
handleChange(e) {
this.setState({ [e.target.name] : e.target.value });
}
Not too sure what's going on in the setState portion of it. The array brackets are throwing me off for some reason. Can anyone elaborate on what this method is doing?
[someExpression] is called a computed property name and is an alternative to writing this:
handleChange(e) {
const stateUpdate = {};
stateUpdate[e.target.name] = e.target.value;
this.setState(stateUpdate);
}
That's new(ish) JavaScript syntax that allows an object literal to compute a property name from an expression. It's effectively the same as:
handleChange(e) {
var state = {};
state[e.target.name] = e.target.value;
this.setState(state);
}
In this case the expression that determines the property name is e.target.name.
Assume you have form with inputs named email and password. You have state variables with same name ‘email’ and ‘password’. Now you have this function handleChange attached for onChange event of input.
When it is called for email, it will set state.email to input value similarly for other fields.
This is a new syntax where you can assign objects key in this way.

In Javascript, can I reference a key value pair from another key value pair? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can a JavaScript object property refer to another property of the same object? [duplicate]
(2 answers)
How do I reference the same Object's properties during its creation? [duplicate]
(5 answers)
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 5 years ago.
var fighters = ['johnjones', 'rondarousey', 'connormcgregor', 'chuckliddel', 'demetriusjohnson'];
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
stable: [fighters, warriors.wrestlers]
}
I believe I can reference fighters from stable, but can I reference wrestlers from stable? In other words, how do I reference a key value pair from a later key value pair within the warriors object. Thank you for any and all help!
You cannot do that until the variable warriors is initialized. The only way is to wait and assign in the next line:
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
}
warriors.stable = [fighters, warriors.wrestlers]
Or use some kind of weird initialization like the one described here. Or use function constructor.

why declared variable has null and some variable has undefined value? [duplicate]

This question already has answers here:
Why global variable 'name' changes to string? [duplicate]
(1 answer)
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed 6 years ago.
can any one please explain why the value of name variable is "" . in java script declared variable always has value undefined?

Categories

Resources