You can use the Array.prototype.includes()
method to check if an array includes a specific value in React JS, just like you would in regular JavaScript. Here’s an example:
import React from 'react';
function App() {
const fruits = ['apple', 'banana', 'orange', 'grape'];
const checkIfIncludes = (value) => {
return fruits.includes(value);
};
return (
<div>
<p>Does the array include 'banana'? {checkIfIncludes('banana') ? 'Yes' : 'No'}</p>
<p>Does the array include 'watermelon'? {checkIfIncludes('watermelon') ? 'Yes' : 'No'}</p>
</div>
);
}
export default App;
In this example, the checkIfIncludes
function uses the includes()
method to determine whether the fruits
array includes the provided value. The function returns true
if the value is present in the array and false
otherwise.
You can replace fruits
with your own array and modify the values you’re checking for as needed.