dev-resources.site
for different kinds of informations.
Controlled vs Uncontrolled Components in React
Published at
2/1/2024
Categories
react
component
webdev
beginners
Author
nameismani
Author
10 person written this
nameismani
open
Controlled vs Uncontrolled Component
Controlled Component:
- In a controlled component, form data is handled by a React component - (state)
- The internal state (not react state) is not maintained.
- Validation is easy.
- Controlled by the parent component.
import React, { useState } from 'react';
const ControlledComponent = () => {
const [name, setName] = useState("");
const handleChange = (e) => {
setName(e.target.value)
};
const handleSubmit = () => {
alert(name)
}
return (
<>
<form onSubmit={handleSubmit}>
<label>Name</label>
<input type="text" value={name} onChange={handleChange} />
<button type='submit'>Submit</button>
</form>
</>
)
}
export default ControlledComponent
Uncontrolled Component:
- Uncontrolled component, form data is handled by a DOM Element - (ref)
- Internal state maintained.
- Validation is much more complex.
- Controlled by DOM itself.
import React, { useRef } from 'react'
const UncontrolledComponent = () => {
const inputRef = useRef(null);
const handleSubmit = () => {
alert(inputRef.current.value)
}
return (
<>
<form onSubmit={handleSubmit}>
<label>Name</label>
<input type="text" ref={inputRef} />
<button type='submit'>Submit</button>
</form>
</>
)
}
export default UncontrolledComponent
component Article's
30 articles in total
Build a note app with JavaScript component.
read article
Key characteristic of Component-Based Architecture
read article
React Component Libraries: Overview of 19 Top Libs
read article
Styling Components in React 🧢
read article
Building a Seamless OTP Input Field in React: A Step-by-Step Guide
read article
MithrilJS component with state management
read article
[Off Topic] Nano introdução do framework Angular para Devs do back
read article
Comparing Vue Component Documentation tools
read article
Laravel 11 Custom Component File Structure
read article
Building Message Component in Vue3
read article
Aplicando paginação em um componente Select
read article
How much does it cost to repair an outdoor LED display?
read article
Global toast in Vue3
read article
Unveiling the Hidden Gem of React: The Power of Compound Components
read article
Controlled vs Uncontrolled Components in React
currently reading
React components -(Class component v/s function component)
read article
3 Ways to Create React Components with Bit
read article
Client or Server component ?
read article
Desenvolvimento de Componentes AssÃncronos com Vue.js
read article
NAND Flash vs NOR Flash: Differences between them
read article
Link Component in React Router
read article
Guia de Components - para quem tem pressa!
read article
How to exchange data between Angular components
read article
Component Testing with Cypress and Reactjs
read article
React - Higher Order Components (HOC)
read article
How to Create Component Library Like Material UI or Mantine UI?
read article
Looking forward to adding Algolia's DOCSEARCH to Mantine DataTable
read article
Cypress Component Testing vs React Testing Library - the complete comparison
read article
Creating Custom Component for NPS Feedback
read article
Maximize your Angular code reusability using <NgTemplateOutlet>
read article
Featured ones: