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.
It returns an array with the state and the function to set the state in this order.
We call setCount
to set the state in App
And we get the count
state in Counter
.
RRH
RRH provides hooks that we can use to get and set React Redux state data.
To install it, we can run:
npm install rrh --save
or
yarn add rrh
Then we can use it by wiring:
import React from "react";
import { useProvider, useSelector } from "rrh";
import { createLogger } from "redux-logger";
import { applyMiddleware } from "redux";
const logger = createLogger();
const initState = {
count: 0
};
const reducer = (state, action) => {
switch (action.type) {
case "INCREMENT":
return { count: state.count + 1 };
default:
return state;
}
};
const Counter = () => {
const selected = useSelector(
{},
(dispatch, props) => ({
increment: () => dispatch({ type: "INCREMENT", payload: 1 })
}),
(state, props) => ({ count: state.count }),
state => [state.count]
);
return (
<div>
{selected.count}
<button onClick={selected.increment}>INC</button>
</div>
);
};
export default function App() {
const Provider = useProvider(() => ({
reducer,
preloadedState: { count: 1 },
storeEnhancer: applyMiddleware(logger)
}));
return (
<div>
<Provider>
<Counter />
<Counter />
</Provider>
</div>
);
}
We create an object with the initial state with the initState
object.
And we create the createLogger
function to return the logger
.
Then we create our reducer to set the state.
In Counter
, we call the useSelector
hook.
The first argument of it is the props.
The 2nd argument has a function to return an object with functions to dispatch actions.
The 3rd argument maps the state the way we want.
The 4th argument has a function with the dependencies to watch for.
The state is in the selected
variable.
In App
, we wrap our components that we want to make accessible to the store with the Provider
.
The Provider
is created with the useProvider
hook that takes a function.
The function returns an object with the reducer
, preloadedState
, and storeEnhancer
.
reducer
has the reducer.
preloadedState
has the initial state.
storeEnhancer
has the Redux middleware we want to run.
Conclusion
@rooks/use-visibility-sensor lets us detect visibility.
Resynced lets us create a shared state for our components.
RRH lets us use Redux with hooks.