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 i…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Pedro Uzcátegui

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 Component</h3>
      <form onSubmit={handleSubmit}>
        <label>Name:</label>
        <input name="name" value={name} onChange={(e) => setName(e.target.value)} />
        <button type="submit">Submit</button>
      </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 Component</h3>
      <form onSubmit={handleSubmit}>
        <label>Name :</label>
        <input type="text" name="name" ref={inputRef} />
        <button type="submit">Submit</button>
      </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!


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Pedro Uzcátegui


Print Share Comment Cite Upload Translate Updates
APA

Pedro Uzcátegui | Sciencx (2022-11-04T01:18:45+00:00) React.js Controlled vs Uncontrolled. Retrieved from https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/

MLA
" » React.js Controlled vs Uncontrolled." Pedro Uzcátegui | Sciencx - Friday November 4, 2022, https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/
HARVARD
Pedro Uzcátegui | Sciencx Friday November 4, 2022 » React.js Controlled vs Uncontrolled., viewed ,<https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/>
VANCOUVER
Pedro Uzcátegui | Sciencx - » React.js Controlled vs Uncontrolled. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/
CHICAGO
" » React.js Controlled vs Uncontrolled." Pedro Uzcátegui | Sciencx - Accessed . https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/
IEEE
" » React.js Controlled vs Uncontrolled." Pedro Uzcátegui | Sciencx [Online]. Available: https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/. [Accessed: ]
rf:citation
» React.js Controlled vs Uncontrolled | Pedro Uzcátegui | Sciencx | https://www.scien.cx/2022/11/04/react-js-controlled-vs-uncontrolled/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.