How to Change an Uncontrolled Input in React?
Last Updated :
27 Jun, 2024
Uncontrolled input refers to form elements such as text fields, checkboxes, or radio buttons, where the value isn't directly managed by the React state. Their values are stored within the DOM itself and are independent of the React state. It can be challenging to change their input. In this article, we will learn about how to change an uncontrolled input in React.
Using useRef Hook
useRef hook of react provides a way to directly access DOM elements. We are going to use useRef hook to change uncontrolled input. First, we are going to create a useRef and attach it to the input element, so we can access it. then by using current.value, we will update it's value.
Syntax:
//Import
import React, { useRef } from 'react';
//Declare
const inputRef = useRef(null);
//Using
<input type="text" ref={inputRef} />
Creating React Application
Step 1: Create a React application using the following command.
npx create-react-app gfg
Step 2: After creating your project folder i.e. gfg, move to it using the following command.
cd gfg
Project Structure
Folder StructurThe updated dependencies in package.json file will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-bootstrap": "^2.9.2",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Approach:
- Import
useRef
from React to create a mutable reference for accessing the input element. - Set up
inputRef
with useRef(null)
to establish a reference that will directly manipulate the input element. - Create a function (
handleClick
) that updates the input's value by accessing inputRef.current.value
and setting it to 'GeeksForGeeks'. - Assign
inputRef
to the ref
attribute of the input element (<input type="text" ref={inputRef} />
). This connects the input element with the inputRef
reference. - Connect the
handleClick
function to a button’s onClick
event. When clicked, it updates the input's value through the inputRef
, demonstrating direct DOM manipulation in React.
Example: This example demonstrate to change an uncontrolled input in react.
JavaScript
/App.js
import React, { useRef } from 'react';
export function App() {
const inputRef = useRef(null);
const handleClick = () => {
inputRef.current.value = 'GeeksForGeeks';
};
return (
<div>
<input type="text" ref={inputRef} />
<button onClick={handleClick}>Change Value</button>
</div>
);
}
Output
Similar Reads
Uncontrolled Vs Controlled Inputs in React Controlled inputs are tied with the state in react and their values are stored controlled by the components using state variables, while the uncontrolled inputs manage their own state and work internally with the DOM.In HTML, form elements such as <input>, <textarea>, <select>, etc
4 min read
When to use an uncontrolled component in React? Uncontrolled Components are the components that are not controlled by the React state or any functions and are handled by the DOM (Document Object Model). So to access any value that has been entered we take the help of refs. In an uncontrolled component, the component manages its state internally.
3 min read
How to add code input in React JS? In this article, we are going to learn how we can add Code Input in React JS. React is a front-end JavaScript library for constructing user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. Approach to add code input: To incorporate our
2 min read
How to Reset a File Input in React.js ? To reset a file input in React JS, we can use the useRef Hook to create a reference to the input element and modify its properties using the DOM API.Prerequisites:NPM & Node JSReact JSReact useRef HookApproach:To reset a file input in react we will create a reset button and handleReset function.
2 min read
How to debounce or throttle input changes in React? Debouncing and throttling are techniques used to limit the frequency of certain actions, particularly useful for handling input changes like those from user interactions. Debouncing:Imagine you have a text input field where users are typing. Each keystroke triggers some action, like filtering search
3 min read