Top React Hooks — Online Status, Timers, and Previous State

Top React Hooks — Online Status, Timers, and Previous State

Useful hooks to do those things.. “Top React Hooks — Online Status, Timers, and Previous State” is published by John Au-Yeung in Dev ...

Hooks contains our logic code in our React app.

We can create our own hooks and use hooks provided by other people.

In this article, we’ll look at some useful React hooks.

React hookedUp

React hookedUp is a library with many hooks to make our lives easier.

To install it, we run:

npm install react-hookedup --save

or:

yarn add react-hookedup

The useMergeState lets us set the state as we did with setState in React class components.

For instance, we can use it by writing:

import React from "react";
import { useMergeState } from "react-hookedup";

export default function App() {
  const { state, setState } = useMergeState({ loading: false });
  return (
    <div>
      <button onClick={() => setState({ loading: !state.loading })}>
        toggle
      </button>
      <p>{state.loading.toString()}</p>
    </div>
  );
}

We have the useMergeState to let us manage object states easily.

It returns an object with the state and setState properties.

state has the current state value.

setState is the function to set the state.

We use it by passing in an object with the property and value.

The usePrevious hook lets us get the previous value from the useState hook.

For instance, we can write:

import React from "react";
import { usePrevious } from "react-hookedup";

export default function App() {
  const [count, setCount] = React.useState(0);
  const prevCount = usePrevious(count);
  return (
    <>
      <button onClick={() => setCount(count => count + 1)}>increment</button>
      <p>
        Now: {count}, before: {prevCount}
      </p>
    </>
  );
}

We use the usePrevious hook by passing in the state value into it.

Then we can get the previous value with the value returned by the hook.

The useInterval hook lets us run setInterval the React way.

For instance, we can write:

import React from "react";
import { useInterval } from "react-hookedup";

export default function App() {
  const [count, setCount] = React.useState(0);
  useInterval(() => setCount(count => count + 1), 1000);
  return <h1>{count}</h1>;
}

We used the useInterval hook with a callback that’s called every second as we specified in the 2nd argument.

javascript programming technology software-development web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Software Developer vs Web Developer | Difference Web Developer & Software Developer

Software Developer vs Web Developer | Difference Web Developer & Software Developer | Software development and web development are normally used interchangeably. Although both include development, a software developer has different responsibilities from that of a web developer.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Offshore Software and Web Application Development Company- MobiWeb Technologies

Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.

Offshore Software and Web Application Development Company- MobiWeb Technologies

Mobiweb Technologies is a prestigious IT company headquartered in Indore providing promising and cost effective software and web development services across the globe.