Converting a string to uppercase in React is done using JavaScript’s built-in toUpperCase()
method. Here’s how you can achieve this:
import React from 'react';
function StringToUppercase() {
const originalString = "Hello World";
const uppercaseString = originalString.toUpperCase();
return (
<div>
<p>Original String: {originalString}</p>
<p>Uppercase String: {uppercaseString}</p>
</div>
);
}
export default StringToUppercase;
In this example, the StringToUppercase
component converts the originalString
to uppercase using the toUpperCase()
method. The converted string is then displayed in the output.
Replace "Hello World"
with your own string, and the component will display both the original and uppercase versions of it.
Keep in mind that toUpperCase()
does not modify the original string; instead, it returns a new string with all characters converted to uppercase.