React.js Controlled vs Uncontrolled

react.js-controlled-vs-uncontrolled

Controlled Components

These components are controlled by react state. For example, if we have a form, and we have the model of that form represented by a react state, and the inputs are linked as two way binding (menaning that changing the input value will change the react state, and that input value is going to be the value of that state) then we have a controlled component.

We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input.

import { useState } from 'react';

function App() {
  const [name, setName] = useState('');

  function handleSubmit() {
    alert(`Name: ${name}`);
  }

  return (
    <div className="App">
      <h3>Controlled Componenth3>
      <form onSubmit={handleSubmit}>
        <label>Name:label>
        <input name="name" value={name} onChange={(e) => setName(e.target.value)} />
        <button type="submit">Submitbutton>
      form>
    div>
  );
}

export default App;

Uncontrolled Components

These components, by definition, are not controlled by the react state, but by the DOM (Document Object Model)

These components uses ref to access their own state.

Usually, uncontrolled components are dependant on the browser, like Inputs of every type, Window Resizing, HTML Canvas Elements, and so on.

import React, { useRef } from 'react';

function App() {
  const inputRef = useRef(null);

  function handleSubmit() {
    alert(`Name: ${inputRef.current.value}`);
  }

  return (
    <div className="App">
      <h3>Uncontrolled Componenth3>
      <form onSubmit={handleSubmit}>
        <label>Name :label>
        <input type="text" name="name" ref={inputRef} />
        <button type="submit">Submitbutton>
      form>
    div>
  );
}

export default App;

For example, In React, an is always an uncontrolled component because its value can only be set by a user, and not programmatically.

Anything that we control with the DOM that can’t be done with a React State, we do with Refs.

React Refs

When you want a component to “remember” some information, but you don’t want that information to trigger new renders, you can use a ref.https://beta.reactjs.org/learn/referencing-values-with-refs

The common way to use refs in functional components is to useRef() hook.

const LoginPage = () => {
    const passwordRef = useRef(null);

    const handleSubmit = (e) => {
        e.preventDefault();
        alert(`This is your password ${passwordRef.current.value}`)

    }

    return(
        <form onSubmit={handleSubmit}>
            <input type="email" name="email"/>
            <input type="password" name="password" ref={passwordRef}/>
        form>
    )
}

Is recommended to initiallize refs to null, since is the DOM is not mounted when loading the page and will not know about the input value beforehand.

We also can forward refs, is not very used in react, but we can use React.forwardRef() to create components that forwards ref to child components.

const Button = React.forwardRef((props, ref)=>(
    <button ref={ref} className="FancyButton">
        {props.children}
    button>
));

// Now we can pass forward refs to this component.
const buttonRef = useRef(null);
<Button ref={ref}>Click me!Button>

Stay tunned for more software engineering posts!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
javascript-generators:-a-beginner’s-guide

Javascript Generators: A Beginner’s Guide

Next Post
amd’s-rdna-3-gpus-are-announced,-introducing-rx-7900-xtx-and-7900-xt

🔥AMD’s RDNA 3 GPUs are announced, introducing RX 7900 XTX and 7900 XT🔥

Related Posts
irremote-程式庫搭配-adafruit-neopoxel-程式庫的問題

IRRemote 程式庫搭配 Adafruit_NeoPoxel 程式庫的問題

IRremote 是大家使用紅外線遙控實驗最常用的程式庫,由於接收紅外線遙控器訊號與時間極度相關,如果你的程式中有耗時較久的動作,就可能會影響到紅外線訊號接收的正確性。舉例來說,像是大家愛用的 WS2812B 燈串,串接越多顆燈,傳輸所有燈顏色的資料所耗的時間就越久,以底下這個採用 Adafruit_NeoPixel 程式庫顯示呼吸燈效果的程式為例: #include #include #include #define DECODE_NEC Adafruit_NeoPixel leds=Adafruit_NeoPixel(16, 7); void setup() { Serial.begin(115200);…
Read More