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.
Melting Pot
Melting Pot has a bunch of hooks that we can use in our React app.
It’s a utility library with many hooks.
To install it, we run:
npm install @withvoid/melting-pot --save
or:
yarn add @withvoid/melting-pot
The useTouch
hook lets us watch for the touching of an element.
To use it, we can write:
import React from "react";
import { useTouch } from "@withvoid/melting-pot";
export default function App() {
const { touched, bind } = useTouch();
return (
<div {...bind}>
<p>Touch this</p>
<p>
Status: <span>{String(touched)}</span>
</p>
</div>
);
}
We use the useTouch
hook, which returns an object with the touched
and bind
properties.
touched
indicates whether the item is touched.
bind
is an object which we use as props for the element we want to watch for touches.
The useUpdate
hook us a replacement of the componentDidUpdate
method in React class components.
We can use it by writing:
import React from "react";
import { useUpdate } from "@withvoid/melting-pot";
export default function App() {
const [count, setCount] = React.useState(0);
useUpdate({
action: () => console.log("state was updated"),
exit: () => console.log("exiting")
});
return (
<div>
<button onClick={() => setCount(count + 1)}>{count}</button>
</div>
);
}
We can use the useUpdate
hook to watch for state updates.
We used the hook with an object passed into it.
action
runs when the state is updated.
exit
runs when it rerenders.
The useWindowScrollPosition
hook lets us watch for the scroll position on our page.
We can use it by writing:
import React from "react";
import { useWindowScrollPosition } from "@withvoid/melting-pot";
export default function App() {
const scrollPosition = useWindowScrollPosition();
return (
<div>
<p style={{ position: "fixed" }}>{JSON.stringify(scrollPosition)}</p>
{Array(1000)
.fill()
.map((_, i) => (
<p>{i}</p>
))}
</div>
);
}
We watch for the scroll position with the hook.
It has the x
and y
properties to show the scroll position’s x and y coordinates.
Now as we scroll, we’ll see the position update.
The useWindowSize
hook lets us watch for the size of the window.
To use it, we write:
import React from "react";
import { useWindowSize } from "[@withvoid/melting-pot](http://twitter.com/withvoid/melting-pot "Twitter profile for @withvoid/melting-pot")";
export default function App() {
const { width, height } = useWindowSize();
return (
<div>
{width} x {height}
</div>
);
}
The width
and height
has the width and height of the window.
Constate
The Constate library lets us share data between components by lifting the state to the top with the context API.
We can install it by running:
npm i constate
or:
yarn add constate
Then we can write:
import React, { useState } from "react";
import constate from "constate";
function useCounter() {
const [count, setCount] = useState(0);
const increment = () => setCount(prevCount => prevCount + 1);
return { count, increment };
}
const [CounterProvider, useCounterContext] = constate(useCounter);
function Button() {
const { increment } = useCounterContext();
return <button onClick={increment}>increment</button>;
}
function Count() {
const { count } = useCounterContext();
return <span>{count}</span>;
}
export default function App() {
return (
<div>
<CounterProvider>
<Count />
<Button />
</CounterProvider>
</div>
);
}
to use it.
We created ou own useCounter
hook to update the count
state.
It returns the state and a function to update it.
Then we use constate
to create a context with the hook.
It returns the context provider and a hook to use the context.
Then in Button
and Counter
, we call useCounterContext
to use the state.
It just returns what we return in the useCounter
hook.
Then we can use the components together in App
without passing props around.
The CounterProvider
is the context provider and it has to wrap around everything.
Conclusion
Melting Pot has various useful hooks.
Constant lets us share context state in an orderly way.