I have a function in my react app that capitalized the first letter of a word or string that is fetched from an API:
// Capitalize the first letter of the word/phrase
const capitalize = (string) => {
if (string) {
return string
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
} else {
return;
}
};
Note: string
is the text that is being fetched from the API. I call the function like so:
capitalize(userInfo.address)
It works as expected but upon logging the results to the console, I realized that it is being run two to three times.
Sandbox: https://codesandbox.io/s/admiring-bose-2qr01j?file=/src/App.js
Is there a way to fix this?
tags to render the UI.
– Nima May 27 '22 at 06:20