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.
The number is in milliseconds.
Then count
is updated every second.
The useTimeout
hook lets us run setTimeout
with a convenient hook.
For example, we can write:
import React from "react";
import { useTimeout } from "react-hookedup";
export default function App() {
useTimeout(() => alert("hello world"), 1500);
return (
<>
<p>hello</p>
</>
);
}
The useTimeout
hook takes a callback which runs after the time given in the 2nd argument.
The number is in milliseconds.
The useOnlineStatus
hook lets us get the current online status.
For instance, we can use it by writing:
import React from "react";
import { useOnlineStatus } from "react-hookedup";
export default function App() {
const { online } = useOnlineStatus();
return <h1>{online ? "online" : "offline"}</h1>;
}
We just call the hook and that returns an object with the online
property.
It indicates whether our app is online or not.
Conclusion
React hookedUp has many useful hooks.
We can set the previous state, use timers, and get online status with the provided hooks.