Categories
JavaScript Answers

How to split string on the first white space occurrence with JavaScript?

Sometimes, we want to split string on the first white space occurrence with JavaScript.

In this article, we’ll look at how to split string on the first white space occurrence with JavaScript.

How to split string on the first white space occurrence with JavaScript?

To split string on the first white space occurrence with JavaScript, we call string’s match method with a regex that match all whitespace characters that comes before other characters.

For instance, we write

const matches = str.match(/^(\S+)\s(.*)/).slice(1);

to call match with /^(\S+)\s(.*)/ to return an array with all the strings whitespace strings that comes after whitespaces.

And then we can get the non-whitespace substring that comes after the first whitespace substring by calling slice with 1.

Conclusion

To split string on the first white space occurrence with JavaScript, we call string’s match method with a regex that match all whitespace characters that comes before other characters.

Categories
React Answers

How to fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript?

Sometimes, we want o fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript.

In this article, we’ll look at how to fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript.

How to fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript?

To fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript, we should make sure we only render valid JSX in our React component.

For instance, we write

export default () => {
  //..
  return (
    <div className="col">
      <h1>Mi Casa</h1>
      <p>This is my house y&apos;all!</p>
      {homes.map((home) => (
        <div>{home.name}</div>
      ))}
    </div>
  );
};

to render elements an an array that returns an array of elements in our component.

We call map with a callback to render the homes array into an array of elements that can be rendered with React.

They’re all valid JSX code so no error will be thrown.

Conclusion

To fix Objects are not valid as a React child. If you meant to render a collection of children, use an array instead with JavaScript, we should make sure we only render valid JSX in our React component.

Categories
React Answers

How to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript?

Sometimes, we want to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript.

in this article, we’ll look at how to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript.

How to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript?

To fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript, we should make sure we call hook functions only on the top level of function components.

For instance, we write

export default () => {
  const classes = useStyles();
  return <AllowanceClass classes={classes} />;
};

to call the useStyles hook at the top of the component.

And then we can use its returned value anywhere in the component.

Conclusion

To fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript, we should make sure we call hook functions only on the top level of function components.

Categories
JavaScript Answers

How to get the class name of ES6 class instance with JavaScript?

Sometimes, we want to get the class name of ES6 class instance with JavaScript.

In this article, we’ll look at how to get the class name of ES6 class instance with JavaScript.

How to get the class name of ES6 class instance with JavaScript?

To get the class name of ES6 class instance with JavaScript, we can use the name property of the class or the constructor.name property of the class instance.

For instance, we write

class SomeClass {
  constructor() {}
}

const s = new SomeClass();
console.log(s.constructor.name);
console.log(SomeClass.name);

to create the SomeClass class.

We get the class name of the SomeClass instance s with s.constructor.name.

And we get the name of the class from SomeClass directly with SomeClass.name.

They’ll both log 'SomeClass'.

Conclusion

To get the class name of ES6 class instance with JavaScript, we can use the name property of the class or the constructor.name property of the class instance.

Categories
JavaScript Answers

How to set URL query params in Vue with Vue-Router?

Sometimes, we want to set URL query params in Vue with Vue-Router.

in this article, we’ll look at how to set URL query params in Vue with Vue-Router.

How to set URL query params in Vue with Vue-Router?

To set URL query params in Vue with Vue-Router, we call router.push with an object that has the query property.

For instance, we write

router.push({ path: "register", query: { plan: "private" } });

to call router.push with an object that has the query property to add a query parameter with key plan and value private to the end of the URL with path register.

Conclusion

To set URL query params in Vue with Vue-Router, we call router.push with an object that has the query property.