Skip to content

Tag: javascript

fetching multiple urls from an api

Basically I am working with a star wars API called swapi (i haven’t worked with APIs before) and I am fetching the data with a simple async-await function. My goal is to get all the movies that API holds (a total of 6 of them) I am using a template tag in HTML and I’m cloning it in javascript and

React state not retuning the latest value of state

The value of this.state.topText and this.state.bottomText is always one change behind. E.g if I type 1234 in topText’s input box, the value of this.state.topText would be 123. The react docs say to fix asynchronous updates we should use the form of setState() that accepts a function rather than an objec…

Validating strings with Java and NodeJS

vo.getResultFun() and cod returns ‘G’ Java validation NodeJS isEqual Why NodeJS return the callback and Java don’t throws the exception? Answer The result of this js part : is false as the result of this java part: So there are several options : Validator.isNullUndefinedEmpty doesn’t w…

How to mock/spy useState hook in jest?

I am trying to spy on useState React hook but i always get the test failed This is my React component: counter.test.js: Unfortunately this doesn’t work and i get the test failed with that message: Answer You need to use React.useState instead of the single import useState. I think is about how the code …

Redirect to another page JavaScript with condition

i have the following code which works just fine in redirecting. what i need is that the code checks the previous page (history) and if it was, for example, https://gmail.com, go to https://google.com and if it was something else go to, for example, https://facebook.com. Answer document.referrer will tell you …