Categories
React Answers

How to return multiple lines JSX in another return statement in React?

Sometimes, we want to return multiple lines JSX in another return statement in React.

In this article, we’ll look at how to return multiple lines JSX in another return statement in React.

How to return multiple lines JSX in another return statement in React?

To return multiple lines JSX in another return statement in React, we can wrap our components with fragments.

For instance, we write

const Comp = () => {
  return [1, 2, 3].map((n, index) => {
    return (
      <React.Fragment key={index}>
        <h3>Item {n}</h3>
        <p>Description {n}</p>
      </React.Fragment>
    );
  });
};

to wrap the h3 and p elements in the React.Fragement component in the map callback.

Then these elements will be rendered without a wrapper element around them.

Conclusion

To return multiple lines JSX in another return statement in React, we can wrap our components with fragments.

Categories
JavaScript Answers

How to check for alphanumeric, dash and underscore but no spaces with JavaScript?

Sometimes, we want to check for alphanumeric, dash and underscore but no spaces with JavaScript.

In this article, we’ll look at how to check for alphanumeric, dash and underscore but no spaces with JavaScript.

How to check for alphanumeric, dash and underscore but no spaces with JavaScript?

To check for alphanumeric, dash and underscore but no spaces with JavaScript, we can use a regex.

For instance, we write

const regExp = /^[a-zA-Z0-9-_]+$/;
const check = "foobar";
if (check.search(regExp) === -1) {
  alert("invalid");
} else {
  alert("valid");
}

to create the regExp regex that matches alphanumeric characters, dashes, and undescores through the string we’re checking.

We call check.search with the regExp to find the first match of the pattern.

If search returns -1, then there’re no matches.

Otherwise, the index of the first match in the string is returned.

Conclusion

To check for alphanumeric, dash and underscore but no spaces with JavaScript, we can use a regex.

Categories
JavaScript Answers

How to set a JavaScript object values dynamically?

To set a JavaScript object values dynamically, we can use the square bracket notation.

For instance, we write

myObj[prop] = value;

to set the myObj‘s prop property to value.

prop is a string with the property name.

Categories
JavaScript Answers

How to create list of objects in JavaScript?

Sometimes, we want to create list of objects in JavaScript.

In this article, we’ll look at how to create list of objects in JavaScript.

How to create list of objects in JavaScript?

To create list of objects in JavaScript, we can put objects in an array.

For instance, we write

const list = [
  { date: "12/1/2022", reading: 3, id: 20055 },
  { date: "13/1/2022", reading: 5, id: 20053 },
  { date: "14/1/2022", reading: 6, id: 45652 },
];

to put 3 objects in the square brackets to populate the list array.

Then we access an object’s value with

console.log(list[1].date);

We use list[1].date to get the 2nd object’s date property value.

Conclusion

To create list of objects in JavaScript, we can put objects in an array.

Categories
JavaScript Answers

How to fix JSON.parse unexpected character error with JavaScript?

Sometimes, we want to fix JSON.parse unexpected character error with JavaScript.

In this article, we’ll look at how to fix JSON.parse unexpected character error with JavaScript.

How to fix JSON.parse unexpected character error with JavaScript?

To fix JSON.parse unexpected character error with JavaScript, we should make sure that we’re parsing a JSON string with JSON.parse.

For instance, we write

const obj = JSON.parse(JSON.stringify(yourJSONObject));

to call JSON.stringify with yourJSONObject to convert the yourJSONObject object to a JSON string.

Then we call JSON.parse to parse the JSON string to the obj object.

Conclusion

To fix JSON.parse unexpected character error with JavaScript, we should make sure that we’re parsing a JSON string with JSON.parse.