How to make text clickable in react js?
June 01, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, I am going to show you. How to make text clickable in react js with code example.
In this article, we will create a simple text clickable in React.js just like you would create in a normal HTML project.
Set the onClick prop on the button element.
When the button gets clicked, update the state variable
Let’s look at the following example to understand how it basically works:
APP.js
import {useState} from 'react';
const App = () => {
const [buttonText, setButtonText] = useState('Click');
function handleClick() {
setButtonText('New text');
}
return (
<div>
<button onClick={handleClick}>{buttonText}</button>
</div>
);
};
export default App;
Step to Run Application: Run the application using the following command
from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/,
you will see the following output:
Check the output of the above code example.
All the best 👍