https://reactjs.org/docs/uncontrolled-components.html
Despite the fact that React's team recommends using controlled components, this is a choice to be made by the developer considering the use case, context, and purpose of implementing the form.
So let's understand the types of components
...and when to use them
As the name suggests these are the components whose value is controlled/driven/directed by React's state within the component.
The state within the component serves as “the single source of truth” for the input elements that are rendered by the component.
import { useState } from "react";
export default function App() {
const [inputValue, setInputValue] = useState(null);
const handleOnChange = (event) => {
setInputValue(event.target.value);
}
return (
<form>
<h1>Controlled Component Example</h1>
<label htmlFor="first-input">Start By Typing in below!</label>
<br/>
<input id="first-input" value={inputValue} onChange={handleOnChange}/>
<br/>
<br/>
<span>State Variable's value:</span>
<br/>
<strong>{inputValue}</strong>
</form>
);
}
In the above code snippet,
we have used a state inputValue
, this is where the value that is entered into the textbox is stored.
Now, we have to change inputValue
on every keystroke, which can be done using onChange
.
We have created an onChange
event handler handleOnChange
which uses setInputValue()
to update the state of the input.
These too as the name suggests, are the components whose value is not controlled by React's state. It is completely controlled by the DOM.
Instead of writing an event handler for the component, we use ref to retrieve the values from the DOM.
import { useRef } from "react";
export default function App() {
const inputValue = useRef(null);
const handleOnClick = () => {
console.log("#### inputValue",inputValue.current.value)
}
return (
<form>
<h1>Controlled Component Example</h1>
<label htmlFor="first-input">Start By Typing in below!</label>
<br/>
<input ref={inputValue} id="first-input"/>
<br/>
<br/>
<button onClick={handleOnClick}>Submit</button>
</form>
);
}
In the above code snippet,
we have created a ref inputValue
and assigned it to the input. We don't need to have an event handler in an uncontrolled component.
Refs are often used as instance properties on a component.
Whenever you submit the form you can see the value on console.
The truth is, you’re supposed to use controlled components as much as possible. That is what the official documentation recommends and that is the easiest way for you to build reusable components.
By working on the higher abstraction layer provided by React, you can worry about your actual business logic without even thinking about the internal workings of the DOM API.
This works for writing the actual application, and the associated unit tests and it even decreases the cognitive load required to mentally parse your code.
Types of Rich Text Editors In the previous post I explained what are RTEs and why are they used, in this post, I am going to throw some light on the types of RTEs, and will make you familiar with the sample content format. https://fifo.im/p/35ghmd1uxln0
Rich Text Editor In recent years, the field of Content Creation and Representation on Digital platforms has seen massive disruption. This transition has shown how companies are racing to build the best experience for content creators in the enterprise domain and trying to find innovative ways to break the traditional molds of sharing and consuming content.