Categories
JavaScript Answers

How to wait until page is completely loaded with Puppeteer and JavaScript?

Sometimes, we want to wait until page is completely loaded with Puppeteer and JavaScript.

In this article, we’ll look at how to wait until page is completely loaded with Puppeteer and JavaScript.

How to wait until page is completely loaded with Puppeteer and JavaScript?

To wait until page is completely loaded with Puppeteer and JavaScript, we call goto with an object with the waitUntil property.

For instance, we write

await page.goto(url, { waitUntil: "domcontentloaded" });

to call page.goto with the url and { waitUntil: "domcontentloaded" } to navigate to the url and wait until the whole page is loaded by setting waitUntil to 'domcontentloaded'.

Conclusion

To wait until page is completely loaded with Puppeteer and JavaScript, we call goto with an object with the waitUntil property.

Categories
JavaScript Answers

How to do list comprehensions like Python with JavaScript?

Sometimes, we want to do list comprehensions like Python with JavaScript.

In this article, we’ll look at how to do list comprehensions like Python with JavaScript.

How to do list comprehensions like Python with JavaScript?

To do list comprehensions like Python with JavaScript, we can use the array filter method.

For instance, we write

const string = "1234-5";
const forbidden = "-";

console.log(
  [...string].filter((c) => !forbidden.includes(c)).map((c) => parseInt(c))
);

to call filter with an array with the characters in string to return a new array with the characters that aren’t in the forbidden string.

We call map with a callback to convert each value to an int with parseInt.

Conclusion

To do list comprehensions like Python with JavaScript, we can use the array filter method.

Categories
React Answers

How to maintain state after a page refresh in React?

Sometimes, we want to maintain state after a page refresh in React.

In this article, we’ll look at how to maintain state after a page refresh in React.

How to maintain state after a page refresh in React?

To maintain state after a page refresh in React, we can save the state in session storage.

For instance, we write

const Comp = () => {
  const [count, setCount] = useState(1);

  useEffect(() => {
    setCount(JSON.parse(window.sessionStorage.getItem("count")));
  }, []);

  useEffect(() => {
    window.sessionStorage.setItem("count", count);
  }, [count]);

  return (
    <div className="App">
      <h1>Count: {count}</h1>
      <button onClick={() => setCount(count + 1)}>+</button>
    </div>
  );
};

to save the latest value of count to session storage with sessionStorage.setItem.

We watch the value of count and call sessionStorage.setItem whenever it changes with the useEffect hook.

To get the item after it refreshes, we call sessionStorage.getItem in the first useEffect callback, which is called with an empty array so it’s run only when the component first mounts.

Conclusion

To maintain state after a page refresh in React, we can save the state in session storage.

Categories
JavaScript Answers

How to convert int to float with JavaScript?

Sometimes, we want to convert int to float with JavaScript.

In this article, we’ll look at how to convert int to float with JavaScript.

How to convert int to float with JavaScript?

To convert int to float with JavaScript, we can use the toFixed method.

For instance, we write

const n = (4).toFixed(2);

to call toFixed with 2 to return 4 as a string rounded to 2 decimal places.

Conclusion

To convert int to float with JavaScript, we can use the toFixed method.

Categories
JavaScript Answers

How to fix Uncaught TypeError: data.push is not a function with JavaScript?

Sometimes, we want to fix Uncaught TypeError: data.push is not a function with JavaScript.

In this article, we’ll look at how to fix Uncaught TypeError: data.push is not a function with JavaScript.

How to fix Uncaught TypeError: data.push is not a function with JavaScript?

To fix Uncaught TypeError: data.push is not a function with JavaScript, we should make sure data is an array.

For instance, we write

const data = [
  {
    name: "ananta",
    age: "15",
    country: "Atlanta",
  },
];

data.push({ name: "Tony Montana", age: "99" });
data.push({ country: "IN" });

to declare the data array.

And then we call data.push to append new entries into data.

Conclusion

To fix Uncaught TypeError: data.push is not a function with JavaScript, we should make sure data is an array.