Top React Hooks — Shared State and Visibility

Top React Hooks — Shared State and Visibility

Includes tutorials and code examples on using hooks for state and effects, for context ... sit at the top-level of your component; Only call Hooks from React functional ... Context allows this sharing of values between components without having to ... This function will simply set the visible state to the opposite of its current state ...

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.

@rooks/use-visibility-sensor

The @rooks/use-visibility-sensor package lets us detect the visibility of our component.

To install it, we can run:

npm install --save @rooks/use-visibility-sensor

Then we can use it by writing:

import React from "react";
import useVisibilitySensor from "@rooks/use-visibility-sensor";

export default function App() {
  const ref = React.useRef(null);
  const { isVisible, visibilityRect } = useVisibilitySensor(ref, {
    intervalCheck: false,
    scrollCheck: true,
    resizeCheck: true
  });
  return (
    <div ref={ref}>
      <p>{isVisible ? "Visible" : "Not Visible"}</p>
      <p>{JSON.stringify(visibilityRect)}</p>
    </div>
  );
}

We use the useVisibilitySensor with a ref that’s passed to the element we want to check the visibility for.

The 2nd argument is an object with various options.

intervalCheck is an integer or boolean that let us whether we check for visibility periodically.

If it’s a number then it’s the interval between each check in milliseconds.

scrollCheck is a boolean to determine whether to check for scroll behavior or not.

resizeCheck lets us check for resize if it’s true .

There’re other options for throttling and more.

Resynced

The Resynced library is a library that lets us share states between multiple components.

To install it, we can run:

yarn add resynced

Then we can use it by writing:

import React from "react";
import { createSynced } from "resynced";

const initialState = 0;
const [useSyncedState] = createSynced(initialState);
const Counter = () => {
  const [count] = useSyncedState();
  return (
    <div>
      <p> {count}</p>
    </div>
  );
};
export default function App() {
  const [_, setCount] = useSyncedState();
  return (
    <div>
      <button onClick={() => setCount(s => s + 1)}>increment</button>
      <Counter />
    </div>
  );
}

We call createSynced with th initial state to return an array with the useSyncedState hook.

Then we use the returned hook in our components.

software-development programming technology javascript 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.