Category Archives: ReactJs

ReactJs Round to two decimal places

We can use native JavaScript method to round to two or more places in React JS. Here we are going to create one example to use toFixed method to format the decimal numbers to 2 decimal places.

ReactJs Round to two decimal placess | JavaScript Example

Let’s create an example to understand the toFixed() method in react.

const PriceCmponent = () =>{
const price = 99.329;
return (
<div>
Price = {price.toFixed(2)}
</div>
);

}

The above exmaple will produce result 99.32.

 Try Example »   

In the same way, you can pass any component as props in react.

React Pass Component As Prop with Props | ReactJs

We can pass component as props in React JS, it is very simple and easy. We can also pass props with components in React with the specified components. Here we are going to create one simple example to pass one component as props into another component. 

React Pass Component As Prop with Props | ReactJs Example

Let’s create two components and pass one component as props in another component.

const ChildComponent = (props) => {
  return (
  <h1>{props.title}</h1>
);
}

const ParentComponent = () => {
render() {

  const MyComponent = this.props.myComponent; 
    return (
      <div> 
         <MyComponent title = {this.props.title} /> 
      </div>
    );
  }

}

const TestComponent = () {

return (
<ParentComponent  myComponent={ChildComponent} />
)

}

In the same way, you can pass any component as props in react. 

ReactJS check element exists in Array


You can use array indexOf method to check whether an element exists in Array or not. Here in this tutorial, we are going to explain how you can use this method to check if array contains value. You can use our online editor to edit and run the code online.


ReactJS check element exists in Array JavaScript Example

Array indexOf method returns -1 if element is not found in array else it returns index of element. Here is an example of Array indexOf method.

Example:


Try it »

Output of above example-

ReactJS check element exists in Array

ReactJS Reload Page


You can use location object reload method to reload the page. Here in this tutorial, we are going to explain how to reload the current page in reactjs. You can also use our online editor to edit and run the code online.


ReactJS Reload Page Example

You can reload the page simply as below-

Example:


Try it »

Output of above example-

ReactJs Reload Page

ReactJs check empty Array or Object


We can use native JavaScript length property check whether the array or object is empty in Reacjs. Here in this tutorial, we are going to explain how you can check null array or object in Reactjs. You can also use our online editor to edit and run the code online.


ReactJs check empty Array or Object Example

You can check empty array or object in Reactjs simply as below-

Example:

Try it »

Output of above example-

ReactJs check empty Array or Object

ReactJs Add Multiple Classes To Element


We can add multiple classes to an element separated by space. Here in this tutorial, we are going to explain how to add multiple classes in an element. You can also use our online editor to edit and run the code online.


ReactJs Add Multiple Classes To Element Example

You can add multiple classes to element(component) simply as below-

Example:

Try it »

In the above all the three classes will be applied, on the same way you can add multiple classes as per your need.

Output of above example-

ReactJs Add Multiple Classes To Element

ReactJs Remove Duplicate Values from Array


There are many ways to remove duplicate items from Array. Here in this tutorial, we are going to explain the simplest and smartest way to remove the duplicate items from array. You can also use our online editor to edit and remove duplicate items from array.


ReactJs Remove Duplicate Values from Array JavaScript

Here is the most efficient native way to remove duplicate values from Array in ReactJs.

Example:

Try it »

In the above example we have used the native JavaScript to remove the duplicate items from an array. Output of above example-

ReactJs Remove Duplicate Values from Array

ReactJs Remove all spaces from string


There are many ways to remove white spaces from a string. We can use use string replace method to remove all spaces from string in Reactjs. Here in this article we are going to explain how you can use native JavaScript method to replace all spaces from string. You can also use our online editor to edit and run the code online.


ReactJs Remove all spaces from string JavaScript Example

Here is an example to remove spaces using replace function-

Example:

Try it »

Output of above example-

ReactJs Remove all spaces from string

ReactJs Get Array Length


We can use Array length method to get the length of array in reactjs. This method is also used to count the number of items in Array. Here in this tutorial, we are going to explain how you can get the length of array ie. count Array items using native JavaScript length method. You can also use our line editor to edit and run the code online.


ReactJs Get Array Length | Count Array Items Method Example

You can use the length method to count the number of items in an array-

Example:


Try it »

If you run the above example it will return 7 ie. total number of items in array. Output of above example will look something like this-

Vue.js Count Array Items JavaScript