Updating Const variable thorough another file [duplicate] - javascript

This question already has answers here:
const or let in React component
(4 answers)
Closed 2 years ago.
I am very new to React and i don't know how to achieve the following case :
I have Variable declared in Config file
Say
Const a= "Some value"
and i want to update that value from different file when certain conditions are met
Assume :
if (true){a= "New Value"}

const is an immutable variable. If you want to have a mutable (changable) variable, use let instead.

You can only initialize a "Const" once!
That's the main difference to all the other ways you can create a variable.

Related

the use of ... element in jsx file [duplicate]

This question already has answers here:
javascript es6 array feature [...data, 0] "spread operator"
(2 answers)
Closed 5 months ago.
I am a beginner at React learning it from an online tutorial.
there is an element used in one of the courses which is ... and can be seen in the following code
handleIncrement = (productId)=>{
const newProducts=[...this.state.products]
const index= newProducts.findIndex(p=> p.id === productId)
newProducts[index].count +=1 ;
this.setState({products : newProducts});
}
its usage seems to be creating an array from products and passing it to the newProducts which is an array now. could you please explain what exactly do ... and when we need it to use?
It's called the spread syntax.
In react, you're not supposed to mutate the state directly. So, if you have an array you want to edit, you will first create a copy of the array with the spread syntax, edit the new array, and finally set the state to the edited copy of the array.
Creating a copy with newProducts = this.state.products is not sufficient in this case as both the variables are pointing to the same object in memory and changing newProducts is the same as changing `this.state.products.

Turn a string into function in JS [duplicate]

This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 2 years ago.
My problem is simple and I couldn't find the proper answer in this forum. My bad...
I want to do that :
const dataReceived = foo;
foo(state);
How can I do that?
I read it is better to avoid eval, and I couldn't get success with new Function.
Thanks for your help!
EDIT
Thanks for your answers.
I work with React.
In my reducer, I have a create_item case.
I can reach action.category, that can be the word 'currency' or 'country'.
What I want to do is to launch either the method createCurrency or createCountry according what is inside action.category.
That's why I tried to join 'create' and 'action.category' to create a dynamic function name.
But it seems to be a poor idea...
The simplest approach is to create an object which contains an entry where:
the key is a string
the value is a function.
Example:
const myObject = {
myFunction: () => { [... DO SOMETHING...] }
}
Subsequently you will be able to invoke the function, using:
myObject.myFunction();
The above becomes more powerful when you use brackets notation.
Example:
const myString = 'myFunction';
myObject[myString]();

Using template literal within JSON objects in React component [duplicate]

This question already has answers here:
Square Brackets Javascript Object Key
(5 answers)
Closed 2 years ago.
I have API data in my React component. It's in the form of
{weather.Wind.Speed.Metric.Value}
Is there any way I can swap Metric out for a variable such as unit.
For example something like
const unit = 'Metric';
{weather.Wind.Speed.${unit}.Value}
That way I can update the variable and show the correct data?
You can use the bracket notation: weather.Wind.Speed[unit].Value.
More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

How to initialize an object with given key names? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
The way I usually do that,
var update = {};
update[name] = data;
update.resolved = true;
where, name is a variable.
I assume that's not the most efficient way of initialization, but it's not possible to use a variable in object notation initialization.
Other possible ways?
You can use computed property names (which is an ES6 feature, but given you tagged your question as such I assume that's not a problem):
var update = {
[name] : data,
resolved : true,
};

Setting and using dynamic variable name JS [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 9 years ago.
I can't figure out how to use the name of a variable previously created with eval, without knowing it.
I mean:
function getName(menu_name, level){
eval("var menu_"+level+"="+menu_name);
}
Now how do I get the name of the variable I just created? Probably keep using eval, but I have to put that name into a $.post call as one of my field name.
Thanks in advice.
If level is an integer, you can treat it as a numerical index for an array:
var menu = [];
menu[level] = menu_name;
If level is anything else, you can treat it as a key for a dictionary/associative array:
var menu = {};
menu[level] = menu_name;
Then, for either of the solutions, if you want to access your menu_name, simply call menu[level].

Categories