Solution

To check if a string contains a substring in react js, use the includes() method it will return true if substring got in string.

Snippet

In this snippet section, we will see an example of checking string contain a substring in react.

const str = "ReactJsSnippet";

console.log(str.includes('Snippet')) // true

Example

In this example, we will check string contain substring or not and print in console and page.

Let’s start coding…

App.js

import { useState, useEffect } from "react";

export default function App() {
  const [str, setStr] = useState("ReactJsSnippet");

  useEffect(() => {
    console.log(str.includes("Snippet"));
  }, []);

  return (
    <div>
      <p>{str.includes("Snippet").toString()}</p>
    </div>
  );
}

Output

includes, String

codesandbox