Categories
JavaScript Answers

How to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript?

Sometimes, we want to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript.

In this article, we’ll look at how to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript.

How to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript?

To fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript, we should make sure our React component returns one root node or fragment.

For instance, we write:

import React from "react";

export default function App() {
  return <div>hello world</div>;
}

to return one div.

Or:

import React from "react";

export default function App() {
  return <>hello world</>;
}

to return a fragment.

Conclusion

To fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript, we should make sure our React component returns one root node or fragment.

Categories
JavaScript Answers

How to create random-salt-hash with crypto with Node.js and JavaScript?

Sometimes, we want to create random-salt-hash with crypto with Node.js and JavaScript.

In this article, we’ll look at how to create random-salt-hash with crypto with Node.js and JavaScript.

How to create random-salt-hash with crypto with Node.js and JavaScript?

To create random-salt-hash with crypto with Node.js and JavaScript, we can use the crypto.randomBytes method.

For instance, we write:

const crypto = require('crypto')
const buf = crypto.randomBytes(16);
console.log(buf)

We call crypto.randomBytes with 16 to create a buffer object with 16 random bytes.

Conclusion

To create random-salt-hash with crypto with Node.js and JavaScript, we can use the crypto.randomBytes method.

Categories
JavaScript Answers

How to extract image src from a string easily with JavaScript?

Sometimes, we want to extract image src from a string easily with JavaScript.

In this article, we’ll look at how to extract image src from a string easily with JavaScript.

How to extract image src from a string easily with JavaScript?

To extract image src from a string easily with JavaScript, we can create a div, set the innerHTML property of the div to the img element string.

And then we can select the img element from the div and get the src property from it.

For instance, we write:

const imgStr = '<img src="http://example.com/img.jpg">'

const getImgSrc = (imgStr) => {
  const div = document.createElement('div')
  div.innerHTML = imgStr
  const img = div.querySelector('img')
  return img.src
}

console.log(getImgSrc(imgStr))

to define the getImgSrc property to a function that takes the imgStr string.

In it, we create a div with createElement.

Then we set div.innerHTML to imgStr.

And then we call div.querySelector with 'img' to get the img element.

Finally, we return the img.src property.

As a result, the console log logs 'http://example.com/img.jpg'.

Conclusion

To extract image src from a string easily with JavaScript, we can create a div, set the innerHTML property of the div to the img element string.

And then we can select the img element from the div and get the src property from it.

Categories
JavaScript Answers

How to set caret position at a specific position in contenteditable div with JavaScript?

Sometimes, we want to set caret position at a specific position in contenteditable div with JavaScript.

In this article, we’ll look at how to set caret position at a specific position in contenteditable div with JavaScript.

How to set caret position at a specific position in contenteditable div with JavaScript?

To set caret position at a specific position in contenteditable div with JavaScript, we select the text node that with the text we want the cursor to be at and set the caret position there.

For instance, we write:

<div contenteditable>
  hello world
</div>

to add a contenteditable div.

Then we write:

const node = document.querySelector("div");
node.focus();
const textNode = node.firstChild;
const caret = 10; 
const range = document.createRange();
range.setStart(textNode, caret);
range.setEnd(textNode, caret);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);

to select the div with querySelector.

We call focus to focus on the div.

Then we get the text node in the div with node.firstChild.

Next, we call document.createRange to create the selection range.

And then we call setStart and setEnd to set the select range.

Next, we call window.getSelection to get the selection.

And we call removeAllRanges to remove all selection.

And then we call addRange with range to move the cursor.

Conclusion

To set caret position at a specific position in contenteditable div with JavaScript, we select the text node that with the text we want the cursor to be at and set the caret position there.

Categories
JavaScript Answers

How to toggle audio play pause with a button or link with JavaScript?

Sometimes, we want to toggle audio play pause with a button or link with JavaScript.

In this article, we’ll look at how to toggle audio play pause with a button or link with JavaScript.

How to toggle audio play pause with a button or link with JavaScript?

To toggle audio play pause with a button or link with JavaScript, we can use a flag to keep track of when the audio is playing or paused.

For instance, we write:

<audio controls src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"></audio>
<button>toggle</button>

to add an audio and button element.

Then we write:

const audio = document.querySelector('audio')
const button = document.querySelector('button')

let isPlaying = false;

button.onclick = () => {
  isPlaying ? audio.pause() : audio.play();
};
audio.onplaying = () => {
  isPlaying = true;
};
audio.onpause = () => {
  isPlaying = false;
};

to select the audio and button elements with querySelector.

Then we set button.onclick to a click event handler function that checks if isPlaying is true or false.

If it’s true, then we pause the audio with audio.pause.

Otherwise, we play the audio with audio.play.

Then we set audio.onplaying and audio.onpause to functions that sets isPlaying to true and false respectively.

Conclusion

To toggle audio play pause with a button or link with JavaScript, we can use a flag to keep track of when the audio is playing or paused.