raddix

useFirstRender

Returns a boolean value indicating whether it's the first render or not.

Installation

Install the custom hook from your command line.

npm i @raddix/use-first-render

Usage

import React from 'react';
import { useFirstRender } from '@raddix/use-first-render';
const App = () => {
const isFirstRender = useFirstRender();
const [count, setCount] = React.useState(0);
return (
<div>
<h1>Rerendered {count} times</h1>
<p>
Is first render: <code>{isFirstRender ? 'yes' : 'no'}</code>
</p>
<button type='button' onClick={() => setCount(count + 1)}>
Rerender
</button>
</div>
);
};

API

Returns

The useFirstRender hook returns a boolean value.

Return valueDescriptionType
isFirstRendertrue for the first render, false for others.boolean