Skip to main content

Posts

Showing posts with the label ReactJS

How to get the HTTP request headers from the initial page request in ReactJS

So, the easier solution is to simply make an AJAX request to the server on page load. As you are using ReactJS, make the request on componentDidMount componentDidMount() {         var req = new XMLHttpRequest();         req.open( 'GET' , document .location, false );         req.send( null );         var headers = req.getAllResponseHeaders().toLowerCase();         headers = headers.split( /\n|\r|\r\n/g ).reduce( function (a, b) {             if (b.length) {                 var [key, value] = b.split( ': ' );                 a[key] = value;             }             return a;         }, {});         console .log(headers);     }

How to detect inactivity on web browser

This is something useful for apps which after a period of inactivity time you need to autosave things, clear localStorage, redirect to a logout URL and many other background things, so it does not matter if you are using VueJS or ReactJS you can add this code <!-- Script --> < script type= 'text/javascript' >   var inactivityTime = function () {     var time;     window .onload = resetTimer;     // DOM Events     document .onload = resetTimer;     document .onmousemove = resetTimer;     document .onmousedown = resetTimer; // touchscreen presses     document .ontouchstart = resetTimer;     document .onclick = resetTimer;     // touchpad clicks     document .onscroll = resetTimer;    // scrolling with arrow keys     document .onkeypress = resetTimer;     window .addEventListener( 'scroll' , resetTimer, true );     let coun...

Adding a new line in a String - Using ReactJS

Let's suppose you get from an API a string like this: { "text" : "FEATURES\n•Contrary to popular belief, Lorem Ipsum is not simply random .\n•BC, making it over 2000 years old. Richard McClintock, a Latin .\n•There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form.\n•If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text." } and you want to print it like this: FEATURES •Contrary to popular belief, Lorem Ipsum is not simply random . •BC, making it over 2000 years old. Richard McClintock, a Latin . •There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form. •If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text . to do so it is quite easy,...

How to read window variables as .json or value key pais outside of /SRC folder on ReactJS during development and production mode

When our application is getting bigger and its settings were hardcoded like the endpoints for instance among other settings, and after making an npm run build you must make an update to any endpoint then you've got not any other choice but to recompile everything again with npm run build and take it to production again, so for this special scenario it is easier to have an external config.js file with all the endpoints inside of it or extra settings your app needs which can be changed through time. to be able to see this working let's create a project running on your terminal  npx create-react-app "give a name to your app, in my case this one was called withproperties " open your application with your favorite code editor and  go to the public folder and let's create over there a file called config.js or any other name you wish to give it, and inside of it paste this: window .endpointone = { "enpointOne" :   "service.com/api/one...