Update variable values ​​in js file

I react very recently, and in my project I have a file that defines the values ​​for the normal variables that I use in the application. Some of these values ​​are taken from cookies, so in this file I have a few lines of code that look like this:

let myCookieValue = new cookies().get("myCookieKey"); 

I do not need to manage cookies, but a general method for updating the values ​​of variables, which in this example contains a cookie value. When the cookie value changes, I would like to call some function to update the value of myCookieValue, but it does nothing like this. I want all the variables declared in this js file to be updated / restored. How can I do this (without making this file a React component)?

+5
source share
1 answer

Cookies are small data items that consist of a name and a value stored on behalf of web browsers. Cookies can be accessed through the document.cookie object, but the interface provided is very primitive.

The expiry property allows cookies to be able to have an expiration date. Thus, in cookies there is an expiration date that will be saved between browsing sessions and is deleted only when the expiration date is reached, or the user indicates this to the browser. The property value can be either the date on which the cookie expires, or a few seconds after the expiration of the cookie.

Clear Cookies

A cookie can be cleared using the clear function, which takes the cookie name as an argument:

 // clear the cookie cookies().clear('myCookieKey'); 

If a cookie has been set for a path or domain other than the current path and domain, they must be passed to the clear function via an optional second parameter:

 // clear the site-wide cookie cookies().clear( 'theme', { path : '/', domain : '.example.com' }); 
+2
source

Source: https://habr.com/ru/post/1275006/


All Articles