How to make the input field not editable in Reactjs?
June 06, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
Today, I am going to show you. How to make the input field not editable in Reactjs with code example.
To make the input field not editable in Reactjs, we can set the onChange={changeHandle} readOnly={true}
to the input field.
Let’s look at the following example to understand how it basically works:
APP.js
import React, { useState } from 'react';
const App = () => {
const [input, setInput] = useState('aguidehub')
const changeHandle = e => {
setInput(e.target.value)
}
return(
<div>
<input type="text" value={input} onChange={changeHandle} readOnly={true} />
</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 👍