Logo

dev-resources.site

for different kinds of informations.

How do I check/uncheck all checkboxes with a button In React Js using useRef() Hook ?

Published at
10/2/2022
Categories
react
checkbox
useref
beginners
Author
radhe65gupta
Categories
4 categories in total
react
open
checkbox
open
useref
open
beginners
open
Author
12 person written this
radhe65gupta
open
How do I check/uncheck all checkboxes with a button In React Js using useRef() Hook ?

How do I check/unchecked all checkboxes with a button In React Js using useRef() Hook ?

In this Post, we will learn about how check/unchecked all checkboxes
using useRef() React Hook.

1>App.js

import logo from './logo.svg';
import './App.css';

import { useRef } from 'react';

function App() {

    const ref = useRef([]);

    const checkboxvalue = (e) => {
        console.log(e.target.value)
    }

    const Unchecked = () => {

        console.log(ref.current.length)
        for (let i = 0; i < ref.current.length; i++) {

            ref.current[i].checked = false;
        }

    }
    const Checked = () => {

        console.log(ref.current.length)
        for (let i = 0; i < ref.current.length; i++) {

            ref.current[i].checked = true;
        }

    }

    return (
        <>
            <div style={{ width: '50%', backgroundColor: '#f7f4f3', marginLeft: '150px', marginTop: '100px', padding:'50px 0px 100px 50px' }}>
            <input ref={(element) => { ref.current[0] = element }} value='12' type='checkBox' onChange={checkboxvalue } /> <br />
            <input ref={(element) => { ref.current[1] = element }} value='123' type='checkBox'  onChange={checkboxvalue } /> <br />
            <input ref={(element) => { ref.current[2] = element }} value='1' type='checkBox'    onChange={ checkboxvalue } /> <br />
            <input ref={(element) => { ref.current[3] = element }} value='1234' type='checkBox' onChange={checkboxvalue } /> <br />

            <button style={{ padding: '5px', backgroundColor: 'green', border: 'none', margin:'5px 5px 5px 5px' }}onClick={Unchecked}>Unchecked</button>

            <button style={{ padding: '5px', backgroundColor: 'red', border: 'none', margin: '5px 5px 5px 5px' }} onClick={Checked}>Checked</button>
            </div> 
        </>
);
}

export default App;

Enter fullscreen mode Exit fullscreen mode

Well done! You now have Done this using useRef() React Hook !

Drop some love by liking or commenting ♥

Reference w3schools

Download Source Code from GitHub Repository

useref Article's
29 articles in total
Favicon
How to create components interfaces using useRef/forwardRef/useImperativeHandle
Favicon
Hooks Behind The Scenes 3, UseRef!!!
Favicon
Mastering React's useRef Hook: Working with DOM and Mutable Values
Favicon
Understanding useRef: A Beginners Guide
Favicon
useRef Hook Explained
Favicon
useRef() in React . . .
Favicon
React State Management: When & Where add your states?
Favicon
Unlocking the Power of useRef: Besic to Advanced Examples for React Developers
Favicon
Leveraging useRef in TypeScript for Efficient DOM Manipulation
Favicon
Unlocking the Power of React Hooks
Favicon
React.useRefの理解を含めるエクササイズ
Favicon
Mastering React's useRef Hook: A Deep Dive
Favicon
Unlocking the Power of useRef in React: 12 Essential Use Cases
Favicon
Série React Hooks: useRef
Favicon
Toggle Password Visibility Using React useRef
Favicon
Stop using useState for everything
Favicon
Using React hooks to develop a Video Player
Favicon
React - How to load a form with the latest data
Favicon
React: сфокусировать поле ввода по чекбоксу
Favicon
How do I check/uncheck all checkboxes with a button In React Js using useRef() Hook ?
Favicon
Multiple item using one ref
Favicon
Closures and useEffects
Favicon
I need help. TypeError: Cannot read properties of undefined (reading 'current')
Favicon
Using React useRef Hook to access immediate past props or state.
Favicon
Compare Props in React Functional Components.
Favicon
Creating infinitely scrolling SPA using React
Favicon
useRef()가 순수 자바스크립트 객체라는 의미를 곱씹어보기
Favicon
Complete useRef() hook with live code examples
Favicon
Useful Patterns with React hooks

Featured ones: