Unverified

Name

React useToggle

About

Basically, what this hook does is that, it takes a parameter with value true or false and toggles that value to opposite. It's useful when we want to take some action into it's opposite action, for example: show and hide modal, show more/show less text, open/close side menu.

Language

Javascript

Rating

Voted: 0 by 0 user(s)

Codevault

draison

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: Not Specified

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

23/09/2022

Important Note

This snippet has the following status:

Unverified

This snippet has not been verified, use with caution and at your own risk. See details provided by author in sidebar and click below to find out more.

React useToggle

 
                    
1import { useCallback, useState } from 'react';
2// Usage
3function App() {
4 // Call the hook which returns, current value and the toggler function
5 const [isTextChanged, setIsTextChanged] = useToggle();
6 
7 return (
8 <button onClick={setIsTextChanged}>{isTextChanged ? 'Toggled' : 'Click to Toggle'}</button>
9 );
10}
11// Hook
12// Parameter is the boolean, with default "false" value
13const useToggle = (initialState = false) => {
14 // Initialize the state
15 const [state, setState] = useState(initialState);
16 
17 // Define and memorize toggler function in case we pass down the component,
18 // This function change the boolean value to it's opposite value
19 const toggle = useCallback(() => setState(state => !state), []);
20 
21 return [state, toggle]
22}

0

Related Snippets

Please see some snippets below related to this snippet..

JavaScript

Unverified

0

Yash's First

Added: 2 days ago

Last Updated: 2 days ago

# Getting Started with Create React App This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). ## Available Scripts In the project directory, you...

JavaScript

Unverified

0

Dynamic Footer Date

Added: 1 year ago

Last Updated: 1 year ago

Change your static timestamp to an automatically updating copyright year or other dynamic timestamp. Just copy a code snippet from below or read more.

JavaScript

Unverified

0

Activate Post Comments BuddyBoss

Added: 1 year ago

Last Updated: 1 year ago

Comments on blogposts outside of buddyboss cannot be posted, because a hidden field forbids the abesenden of the comment.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

0

Displaying Author Lists with Pagination

Added: 1 year ago

Last Updated: 1 year ago

n this tutorial, we are going to show you how to create a custom page that display a list of users using a set of these template tags.

JavaScript

Unverified

0

React useToggle

Added: 1 year ago

Last Updated: 1 year ago

Basically, what this hook does is that, it takes a parameter with value true or false and toggles that value to opposite. It's useful when we want to take some action into it's opposite action, for ex...